noufalcep
noufalcep

Reputation: 3536

Laravel - How to get file extention from a path?

I am using File class for getting extention of image url.

File::extension($image_path)

But my image URL is example.com/images/new.png?v=21212154

It returns png?v=21212154

How to get the value png only?

Upvotes: 0

Views: 931

Answers (3)

Dmitry G
Dmitry G

Reputation: 93

Try to use pathinfo():

$extension = pathinfo($image_path, PATHINFO_EXTENSION);

or

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

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try this way:

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

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

OR Try with below code.

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

Upvotes: 1

u_mulder
u_mulder

Reputation: 54841

strstr will help you get values before some symbol:

$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;
}

Upvotes: 1

Related Questions