Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, How to get Mime Type - Mission Impossible?

Is there any decent way in PHP to get the mime type?

I have been searching extensively the past few hours and it seems like there are three main ways, which each way having problems:

  1. mime_content_type() This is deprecated, alot of the times not installed, and if installed will sometimes not find the mime.magic file.

  2. file_info Wasn't installed on the hosts I tried, doesn't seem to have very good support. Is an extension (PECL).

  3. shell_exec(file -ib . $file) Doesn't work on windows servers. I tried it on a linux server and it gave me "image/x-3ds2" for a php file. What the hell is that!!!

What is a good, almost bullet proof way to get the mime type of a file?

like image 472
mysqllearner Avatar asked May 20 '10 06:05

mysqllearner


1 Answers

As workaround you can use the "mime.php" extension from http://upgradephp.berlios.de/ It simulates the mime_content_type() if not available. Made specifically for such cases.

You can install your private mime.magic file and force it with ini_set("mime_magic.magicfile"). This is recommended anyway, so you have the desired settings available.

like image 99
mario Avatar answered Sep 20 '22 20:09

mario