Why does the Laravel getMimeType () method identify the file as “application / octet stream” when the file has an attribute of type “audio / mpeg”?

I am trying to upload an MP3 file to a Laravel application and have encountered a problem, although despite the fact that the file has the attribute “audio / mpeg”, it loads as “application / octet stream” (.bin). When I try to die and upload a file on server code with:

dd($request->file('file')); 

I get:

 UploadedFile {#187 â–Ľ -test: false -originalName: "CUS12309821-20-AUG-2016-13-48-13.mp3" -mimeType: "audio/mpeg" -size: 47000471 -error: 0 path: "/private/var/folders/c7/6ws0lxy95dd_lhz1k067_zkc0000gn/T" filename: "phpyZCsbU" basename: "phpyZCsbU" pathname: "/private/var/folders/c7/6ws0lxy95dd_lhz1k067_zkc0000gn/T/phpyZCsbU" extension: "" realPath: "/private/var/folders/c7/6ws0lxy95dd_lhz1k067_zkc0000gn/T/phpyZCsbU" aTime: 2016-09-20 12:56:00 mTime: 2016-09-20 12:56:00 cTime: 2016-09-20 12:56:00 inode: 4565593 size: 47000471 perms: 0100600 owner: 501 group: 20 type: "file" writable: true readable: true executable: false file: true dir: false link: false } 

See how, when using this method, it really says that the file attribute for mimeType is the correct "audio / mpeg" format. However, when I call the getMimeType () method in the file after loading it, I get:

 "application/octet-stream" 

Here's the code in the routed method:

 /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $file = $request->all(); $filePath = Storage::putFile('file', $request->file('files')); dd($request->file('file')->getMimeType()); $file['path'] = Storage::url($filePath); $file['size'] = Storage::size($filePath); $file['type'] = $request->file('file')->getMimeType(); return $file; } 

This problem seems to be unique in that I use the Laravel framework, where others with this problem use vanilla PHP. In addition, other excel files may possibly portray themselves as an application / octet stream instead of an excel file. Finally, I believe this may be a problem with the guess () method that getMethodType () calls. Perhaps someone with more Laravel experience can confirm this.

+6
source share
2 answers

The UploadedFile object is ultimately extended from Symfony\Component\HttpFoundation\File\UploadedFile , which gets / sets mimeType from The type of the file as provided by PHP .

To access this type of mimeType you need to call $file->getClientMimeType()

However, in the Symfony docblock for a function, it offers:

The mime client type is extracted from the request from which the file was downloaded, so it cannot be considered safe.

For a reliable mime type, use getMimeType () instead (which guesses about the mime type based on the contents of the file).

In your case, however, $file->getMimeType() , which should be trusted and guess the mime type from the content, however, it returns something as if it could not determine the mime type, being "application / octet-stream"

Additional Information

To help you decide. Basically, getClientMimeType() will return the mime type specified by the browser.

The getMimeType call guesses the mime type using two different methods that I can see:

  • Using the mime binary method, looking at the result of the following file -b --mime %s 2>/dev/null command, if supported.

  • The second method uses finfo_open if it exists inside php.

If both 1. and 2. exist in your system, then from what I see there will be a preference, and 1. will be a backup.

I would personally support getMimeType() results for security. However, it would be another interesting question: "How reliable is the detection of the mime-browser type and what methods are used" :-)

Updated example

I will give an example for you.

For me, doing a check on "DropboxInstalled.dmg", here are my results:

  • using file -b --mime DropboxInstaller.dmg application/octet-stream returned from the command line (terminal)

  • using finfo_open functionality

 $finfo = new \finfo(FILEINFO_MIME_TYPE); echo $finfo->file('./DropboxInstaller.dmg'); 

returns application/x-iso9660-image

+7
source

I had this problem with Laravel 5.4. I fixed post_max_size and upload_max_filesize in my php.ini to a higher value.

After that, I really had to perform a hard restart of OSX before it really reflected in the application.

+2
source

All Articles