Trying to get mime type on docx files results in application/octet-stream

I'm trying to validate file upload on Laravel (docx only) and it keeps failing. After some research it got down to both $request->file('file')->getMimeType() and mime_content_type($request->file('file')->path()) returning application/octet-stream.

All the other types I checked return proper mime types, the problem is just with docx. The site is running on apache2.

Is it possible to set the system up to properly identify docx files? And if possible is it reliable at all or should I drop it and make a docx validator using something like PhpWord?

Thanks!

Upvotes: 1

Views: 2176

Answers (2)

user23841468
user23841468

Reputation: 1

Is it possible to set the system up to properly identify docx files? And if possible is it reliable at all or should I drop it and make a docx validator using something like PhpWord?

Laravel uses Symfony, and in turn Symfony has a mime-type guesser which uses the file command: See the Symfony source (the mime type sent by the browser is unsafe, so it tries to guess it). For some docx/xlsx/etc files, file is unable to guess the type of the docx (so returning application/octet-stream).

You can verify this by manually putting the file on the server and then checking its guessed mime type by running file --mime-type path/tofile

The file command uses your uses your system's magic database (often at /etc/magic, you can find out where your magic database is by typing file --version).

The fix is to either update your system so you have a more recent version of file and the magic database, or edit your /etc/magic file so that includes the contents the following file: https://github.com/file/file/blob/master/magic/Magdir/msooxml

Upvotes: 0

Tutti Frutti
Tutti Frutti

Reputation: 101

A MIME attachment with the content type "application/octet-stream" is a binary file. Typically, it will be an application or a document that must be opened in an application.

Upvotes: 1

Related Questions