tags:

views:

1001

answers:

2

mime_content_type() is deprecated.

How can I find the MIME type of a local file using PHP5 but without using this deprecated method or the PECL fileinfo extension?

Edit: That's what I was afraid of. It's unfortunate that they deprecated a built-in function in favour of one that requires an extension that isn't always available.

+1  A: 

If you can't use the fileinfo extension, and you don't want to use mime_content_type, your options are limited.

Most likely you'll need to do a lookup based on the file extension. mime_content_type did something a bit more intelligent and actually looked for special data in the file to determine the mime type.

Michael Pryor
A: 

The getID3() library is a quick and easy works-most-of-the-time option. Originally named for a project to obtain MP3 ID3 data, the library does two hecks of a lot more than that and is quite convenient for all sorts of common or odd file meta data tasks.

I've used it to get the MIME types of files for online image and video tools. In all the testing I've done I've not seen getID3 get the MIME type wrong.

I've also used it to check if QuickTime videos have streaming hints. I mention this as an example of versatility.

A second more time consuming option is to roll your own MIME type checker as already suggested. If you have a MIME magic file you can go a little further than a lookup on the file extension by comparing the first n bytes of file data against a first-n-bytes to MIME type lookup table derived from your MIME magic file.

A typical MIME magic file will contain in excess of 500 sets of MIME types which might result in slow comparisons (lots of checks to make). Hard-coding the 10 most common MIME type checks in your home rolled solution will help there.

Jon Cram