Get an image extension from an uploaded file in La

2020-02-08 23:39发布

I have been trying to get the extension from an uploaded file, searching on google, I got no results.

The file already exists in a path:

\Storage::get('/uploads/categories/featured_image.jpg);

Now, How can I get the extension of this file above?

Using input fields I can get the extension like this:

Input::file('thumb')->getClientOriginalExtension();

Thanks.

7条回答
家丑人穷心不美
2楼-- · 2020-02-08 23:47

Yet another way to do it:

//Where $file is an instance of Illuminate\Http\UploadFile
$extension = $file->getClientOriginalExtension();
查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-08 23:53

The Laravel way

Try this:

$foo = \File::extension($filename);
查看更多
唯我独甜
4楼-- · 2020-02-08 23:57

You can use the pathinfo() function built into PHP for that:

$info = pathinfo(storage_path().'/uploads/categories/featured_image.jpg');
$ext = $info['extension'];

Or more concisely, you can pass an option get get it directly;

$ext = pathinfo(storage_path().'/uploads/categories/featured_image.jpg', PATHINFO_EXTENSION);
查看更多
放我归山
5楼-- · 2020-02-08 23:58

If you just want the extension, you can use pathinfo:

$ext = pathinfo($file_path, PATHINFO_EXTENSION);
查看更多
虎瘦雄心在
6楼-- · 2020-02-09 00:01

Tested in laravel 5.5

$extension = $request->file('file')->extension();
查看更多
狗以群分
7楼-- · 2020-02-09 00:01

Or can use the Extension Splitter Trickster::getExtention() function of https://github.com/secrethash/trickster

Trickster::getExtention('some-funny.image.jpg'); It returns jpg

查看更多
登录 后发表回答