Get an image extension from an uploaded file in Laravel

PhpLaravelFilesystems

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

Tested in laravel 5.5

$extension = $request->file('file')->extension();

Solution 2 - Php

The Laravel way

Try this:

$foo = \File::extension($filename);

Solution 3 - Php

Yet another way to do it:

//Where $file is an instance of Illuminate\Http\UploadFile
$extension = $file->getClientOriginalExtension();

Solution 4 - Php

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);

Solution 5 - Php

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

$ext = pathinfo($file_path, PATHINFO_EXTENSION);

Solution 6 - Php

 //working code from laravel 5.2

 public function store(Request $request)
 {
          $file = $request->file('file');
            if($file)
            {
                    $extension =  $file->clientExtension();
            }
            echo $extension;
 }

Solution 7 - Php

return $picName = time().'.'.$request->file->extension();

The time() function will make the image unique then the .$request->file->extension() gets the image extension for you.

You can use this it works well with Laravel 6 and above.

Solution 8 - Php

Do something like this:

if($request->hasFile('video')){

    $video=$request->file('video');
    $filename=str_random(20).".".$video->extension(); 
    $path = Storage::putFileAs(
            '/', $video, $filename
     );
    $data['video']=$filename;
 }

Solution 9 - Php

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionItalo BorgesView Question on Stackoverflow
Solution 1 - PhpAmir HosseinView Answer on Stackoverflow
Solution 2 - PhpAlfredo EMView Answer on Stackoverflow
Solution 3 - PhpDaniel Teleginski CamargoView Answer on Stackoverflow
Solution 4 - PhpJeremy HarrisView Answer on Stackoverflow
Solution 5 - PhpaynberView Answer on Stackoverflow
Solution 6 - PhpBCPNAYAKView Answer on Stackoverflow
Solution 7 - PhpDivinoView Answer on Stackoverflow
Solution 8 - PhpHasnain Abid KhanzadaView Answer on Stackoverflow
Solution 9 - PhpsecrethashView Answer on Stackoverflow