I'm having trouble detecting the mime type of a simple PNG file with fileinfo, with PHP 5.3.8 installed on a CentOS server.
The problem
Basically, if I have the following code :
<?php
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
$handle = finfo_open($const, '/usr/share/file/magic.mime');
$result = finfo_file($handle, '/var/vhosts/v4dev/public/Melvin.png');
echo $result;
unset($handle);
?>
As you can see the file is a PNG image. The header bytes of the file has been checked and are correct. But this page outputs an incorrect result :
application/octet-stream
I guess this is because the file type could not be detected, and fileinfo returned the default response, but I found no way to check that.
What I tried
To check the magic.mime file correctness, I used the file console command :
file -m /usr/share/file/magic.mime /var/vhosts/v4dev/public/Melvin.png
Which returned the expected result :
/var/vhosts/v4dev/public/Melvin.png: image/png
I also tried another magic.mime file provided with Apache, but the problem remains.
I tried specifying the magic.mime file through the default fileinfo location (with symlink and copy of the file), with the MAGIC
environment variable, and by specifying the file path in the finfo_open
call (as above).
I tried updating PHP.
... and now i'm out of options.
If anyone could help me with this, i'd send waves of happiness through space so he/her can live happily ever after.
Cheers
So why not call the file
command from your script?
$result = `file -bm /usr/share/file/magic.mime /var/vhosts/v4dev/public/Melvin.png`
Sure, it's not perfect, but it's an option.
来源:https://stackoverflow.com/questions/7880678/mime-type-detection-fails-with-fileinfo-on-php-5-3-8