sunrise's avatar

laravel can not get image's extension

I am using Laravel 5.3,

I crop an image via javascript on the front end and upload it to back end via ajax,

The headers is like this:

enter image description here

And dd() the image:

    public function changeAvatar(Request $request)
    {
    
        $file = $request->file('croppedImage');
        dd($file);
    
    }

The result is like this: enter image description here

But,dd() the extension of the image:

    public function changeAvatar(Request $request)
    {
        $file = $request->file('croppedImage');
        $extension = $file->getClientOriginalExtension();
        dd($extension);
    }

The result is ''.

Why is it ?

0 likes
2 replies
Snapey's avatar

Because you did not provide an extension? Try changing 'croppedImage' to 'croppedImage.png' ?

tonysmessias's avatar

Checking the implementation of Symphony's Uploadedfile::getClientOriginalName() method, it checks the originalName attribute, which in your case is "blob".

You could try using the the UploadedFile::guessClientExtension() instead (see here), which will try to guess it using the mimetype.

Please or to participate in this conversation.