Getting mime type from file name in php

I was trying to get mimeType of file using mime_content_type() and get_mime_type() but it was not working.

Here this works for me

$file = $request->file('FILE_NAME_IN_REQUEST');
$mimeType = $file->getClientmimeType();

*NOTE: function will receive $request which is coming from .blade file using the input field having type = file


If you check the documentation, you can see that you are not doing anything wrong. But if you do a bit more research:

https://stackoverflow.com/a/3664655/3784145

you can see that the mime type you get is correct, but the extension doesn't need to match with the mime type as explained here:

http://nl3.php.net/manual/en/function.mime-content-type.php#85879

I would therefore use the files suffix to determine the files mime type. (as seen in the first example)

Tags:

Php

Mime Types