Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an image extension from an uploaded file in Laravel

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.

like image 946
Italo Borges Avatar asked Jul 15 '16 19:07

Italo Borges


People also ask

How can I get file extension in PHP?

There are a few different ways to extract the extension from a filename with PHP, which is given below: Using pathinfo() function: This function returns information about a file. If the second optional parameter is omitted, an associative array containing dirname, basename, extension, and the filename will be returned.

How do I move an image into a folder in Laravel?

Below the Laravel approach for moving after upload. From the documentation: $request->file('photo')->move($destinationPath); $request->file('photo')->move($destinationPath, $fileName); photo is the name of your file upload input element.


2 Answers

Tested in laravel 5.5

$extension = $request->file('file')->extension(); 
like image 103
Amir Hossein Avatar answered Sep 20 '22 14:09

Amir Hossein


The Laravel way

Try this:

$foo = \File::extension($filename); 
like image 44
Alfredo EM Avatar answered Sep 18 '22 14:09

Alfredo EM