X
Business

PHP5 - MIME type quirks

It has been a while since I did any web development, and I recently decided to get back into PHP development. After downloading the latest version of PHP for my IIS server I proceeded to do some tinkering just to get back into the feel of the language.
Written by williamc , Contributor

It has been a while since I did any web development, and I recently decided to get back into PHP development. After downloading the latest version of PHP for my IIS server I proceeded to do some tinkering just to get back into the feel of the language. Just for grins I decided to figure out how to send a directory listing to the browser, adding links for any image files that were found so that the user could click on them and display the image in their browser. Simple, right? Well apparently not. I must have spent about 4 hours last night on Google trying to find out why the MIME typing was not working correctly, nor the various PHP extension libraries being loaded properly. I finally managed to figure it out this afternoon:

PHP comes with two extension modules for determining the MIME type of a file: Fileinfo and Mimetype. Both require a mime.magic file that stores the MIME type definitions, but they access it in two different ways. Fileinfo requires that the path to the magic file be passed to the finfo_open function. Mimetype, on the other hand, uses a setting in the php.ini file that identifies the magic file.

Now the only reason why I mention anything about this topic is that judging from the plethora of Google hits I came across while trying to solve my dilemma I realized that this is not well-documented anywhere, not even in the PHP documentation. So here is a concise explanation:

The MIME magic files If you go to the GnuWin32 project page on SourceForge you can download the MIME magic files and unzip the archive to your PHP installation folder.

mkdir C:\PHP\magic ... unzip the MIME files into C:\PHP\magic should now contain: magic magic.mgc magic.mime magic.mime.mgc

Using Fileinfo In your php.ini file, make sure that the DLL gets loaded automatically by PHP:

... add to the end of the file where other extensions are loaded extension=php_fileinfo.dll

Now within your PHP file you can do the following:

$finfo = finfo_open( FILEINFO_MIME, "C:/PHP/magic/magic" ); $mimeType = finfo_file( $finfo, "C:/PHP/php.gif" ); finfo_close( $finfo ); echo $finfo; .... should display image/gif

Using Mimetype First, make sure that the extension is loaded and that it knows where the magic file is located.

mime_magic.magicfile="C:/PHP/magic/magic.mime" extension=php_mime_magic.dll

Now in your PHP page you can do this:

echo mime_content_type("C:/PHP/php.gif"); ... again, image/gif

That's all there is to it. Now, a couple of things to mention ... the Mimetype extension is marked as deprecated with the Fileinfo extension being the preferred method for determining file MIME type. I'm not sure why this is the case, as for me it makes more sense to have the MIME magic files be a system-wide setting, but according to the PHP extensions documentation Fileinfo is the way to go. The other thing to note is that Fileinfo uses the C:/PHP/magic/magic file whereas Mimetype uses C:/PHP/magic/magic.mime. It is very important to get these right. If you switch them up it will not work. I tried this to make sure that that is the case and was unable to get the MIME type.

For anyone out there who does regular PHP development, you all probably knew most of this already. For all those who are new to PHP or re-learning it like me, I hope this helps clarify and resolve any problems you have encountered with this set of extensions.

Editorial standards