I have had a similar problem. FileInfo on board my PHP 5.4.7 (in XAMPP under Windows) is able to detect .flv
file as video/x-flv
, but fails on .mkv
video (should detect it as video/x-matroska
, but returns application/octet-stream
instead) and many other non-typical multimedia formats.
To solve this problem I dropped FileInfo at all and reverted back to old, good mime-by-extension method, using following function:
public static function getMimeTypeByExtension($file)
{
$extensions = require('mimeTypes.php');
if(($ext=pathinfo($file, PATHINFO_EXTENSION))!=='')
{
$ext=strtolower($ext);
if(isset($extensions[$ext]))
return $extensions[$ext];
}
return null;
}
Contents of mimeTypes.php
you can find at pastebin.com. It is compiled by me, by merging Apache SVN's mime.types
file and similar mimeTypes.php
array from Yii Framework. Resulting set has over 1000 relations between file extension and coresponding MIME-type.
Importan note: Before using this solution, you must carefully consider, what purpose you need it for? MIME-type based on file extension can be very easily spoofed, by simply changing that extension. So any security-related piece of code should be kept far as possible from my solution.
I needed it only for simple purpose of providing correct MIME-type of video file for flowplayer. I assumed that, if user change extension of such file to something else, then flowplayer will simply fail to playback that movie.