redbutton's avatar

Laravel 5.4 UploadedFile::getMimeType returns incorrect mimetype

I want to upload a .eps file. to do this I have a form to upload multiple images and this all works fine. When I upload a .eps. file, the Validator class fails. This is my code for the validator: // $file is an instance of UploadedFile $validator = \Validator::make( [ 'file' => $file ], [ 'file' => 'required|mimes:jpg,jpeg,png,tiff,psd,ai,eps,pdf' ] ); \dd($validator->fails()); if ($validator->fails() ){ ... }

This is because the mimetype (according to \Symfony\Component\HttpFoundation\File\File::getMimeType is application/octet-stream.

When I dd $file, this is the result: UploadedFile {#410 ▼ -test: false -originalName: "2365 MD8300PC03.eps" -mimeType: "application/postscript" -size: 310578 -error: 0 #hashName: null path: "/private/var/tmp" filename: "phpI2fYV6" basename: "phpI2fYV6" pathname: "/private/var/tmp/phpI2fYV6" extension: "" realPath: "/private/var/tmp/phpI2fYV6" aTime: 2017-07-11 12:40:54 mTime: 2017-07-11 12:40:52 cTime: 2017-07-11 12:40:52 inode: 21493674 size: 310578 perms: 0100600 owner: 504 group: 0 type: "file" writable: true readable: true executable: false file: true dir: false link: false }

When I dd $file->getClientMimeType, the result is application/postscript, which is the correct mimetype.

The only option I can think of at the moment is to override the File class so I can check the mimetype before it's returned by the method.

0 likes
1 reply
gelkin's avatar

I have a similar problem: when I upload a file with extension .svg using UploadedFile class, when I call store() method on it, it calls standard File's class method to get mimeType for output file which uses MimeTypeGuesser and guesses mime by filename, not by mimeType field which I explicitly specified when creating UploadedFile instance. It looks like a bug.

UPD: I got hints on how to solve my problem here https://stackoverflow.com/questions/39594854/why-does-laravels-getmimetype-method-identify-a-file-as-application-octet-st

Please or to participate in this conversation.