Laravel - 如何从路径获取文件扩展?

时间:2018-03-16 06:30:59

标签: php laravel laravel-5

我正在使用File类来获取图片网址的扩展。

File::extension($image_path)

但我的图片网址为example.com/images/new.png?v=21212154

返回png?v=21212154

如何仅获取值png

3 个答案:

答案 0 :(得分:1)

你应该这样试试:

$url = 'example.com/images/new.png?v=21212154';
$data = explode('?',$test);
$data = explode('/',$data[0]);
$filename = $data[2];

$extention = File::extension($filename);

或尝试使用以下代码。

$result = File::extension($image_path)
$url =  strstr($result,"?");
echo str_replace($url,"",$result);

答案 1 :(得分:1)

strstr会帮助您在某个符号前获取值:

$extension = 'png?v=21212154';
$real_extension = strstr($extension, '?', true);
// but if there's no '?' in `$extension`,
// `$real_extension` will be false
if ($real_extension) {
    $extension = $real_extension;
}

答案 2 :(得分:0)

尝试使用pathinfo():

$extension = pathinfo($image_path, PATHINFO_EXTENSION);

$info = pathinfo(image_path);
$extension = $info['extension'];