MIME type detection in PHP

I am looking for the best (probably on most PHP 5.2 and still actively developing) way to detect MIME type files in PHP.

I know the mime_content_type() method and the Fileinfo extension, however mime_content_type() is unreliable and deprecated, and Fileinfo is not part of the standard PHP installation (5.2).

Do I have any other options?

+6
mime-types php mime
source share
4 answers

Have you looked into this PEAR package?

http://pear.php.net/package/MIME_Type

+5
source share

(lol sorry, I realized that this was asked a few months ago when I was finishing ... well, maybe also add this ...)

If it is a * nix server, you can use the file command.

 file -bi <filepath> 

You will need to use "exec" in PHP to do this, I think? I am new to PHP, so don't quote me on this, but ...

 $content_type = exec("file -bi " . escapeshellarg($filepath)); 

I have not tested it, so you may need to avoid the path line and format the output.

I do not know if it will be more reliable than other methods.

+11
source share

You can make a subquery for the web server you are running on, then parse the header to get this type of mime. Since this is slow, use curl to get the headers, not the full request. This is important if you are looking at large files.

If you are using Apache and using php as a module, look at the VIRTUAL function. this is a faster way to make a subquery than using curl.

Otherwise, you can search for the mime type by extension. This will make all well-named files work.

0
source share
-2
source share

All Articles