69

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.

1
  • 1
    getClientOriginalExtension() gets wrong extension (user could send jpeg with png extension), so everytime you'd rather use ->extension() method - it does not matter what class both UploadedFile and File have same method.
    – jave.web
    Feb 19, 2019 at 9:52

9 Answers 9

99

Tested in laravel 5.5

$extension = $request->file('file')->extension();
7
  • 3
    Thanks for taking the time to contribute an answer. It’s because of helpful peers like yourself that we’re able to learn together as a community. Here are a few tips on how to make your answer great: How do I write a good answer.
    – Brien Foss
    Feb 25, 2018 at 22:46
  • 1
    This is the best awnser. Works on Laravel 5.3+ Apr 18, 2019 at 7:19
  • It is slightly annoying that it bases itself on the mime type and thus returns jpeg if the extension is jpg
    – Surya
    Apr 22, 2020 at 9:49
  • @Surya What's wrong with that? Mar 2, 2022 at 16:09
  • @HashimAziz It means that if you are trying to find a file that is named filename.jpg you won't be finding it as it will look for filename.jpeg
    – Surya
    Mar 2, 2022 at 23:43
85

The Laravel way

Try this:

$foo = \File::extension($filename);
3
  • 1
    Thanks, @Alfredo EM. I tried but just get "". The solution above works fine. Jul 15, 2016 at 19:49
  • 1
    $extension = \File::extension('/uploads/categories/featured_image.jpg') really dont work =( ?
    – Alfredo EM
    Jul 15, 2016 at 20:02
  • 3
    Copy and paste \File::extension('/uploads/categories/featured_image.jpg') ; and will return jpg
    – Alfredo EM
    Jul 16, 2016 at 17:40
67

Yet another way to do it:

//Where $file is an instance of Illuminate\Http\UploadFile
$extension = $file->getClientOriginalExtension();
3
  • 1
    this is for laravel v 5
    – Abed Putra
    Jan 26, 2018 at 2:13
  • 9
    Danger: this gets CLIENT extension, not the real mime-type extension, to get that use extension() instead
    – jave.web
    Feb 19, 2019 at 9:54
  • I have filename.jpg. When using ->extension() it returns jpeg while ->getClientOriginalExtension() returns jpg which is what I want.
    – Dale Ryan
    Sep 8, 2022 at 7:40
41

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

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

$ext = pathinfo($file_path, PATHINFO_EXTENSION);
5
 //working code from laravel 5.2

 public function store(Request $request)
 {
          $file = $request->file('file');
            if($file)
            {
                    $extension =  $file->clientExtension();
            }
            echo $extension;
 }
4
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.

1

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.