
Untitled
By: a guest on
Aug 19th, 2012 | syntax:
PHP | size: 1.39 KB | hits: 18 | expires: Never
function getMimeType($file_name, $upload_type) {
$mime_type = '';
/*
Fileinfo library might fail to determine the mime type in PHP versions prior to 5.3
due to troubles locating the magic file. In this case it will return application/octet-stream.
On some hosting envoronments application/x-character-device is returned for all file types.
So we will assume these values are incorrect and will keep trying to determine the mime type.
We will use the FILEINFO_MIME constant instead FILEINFO_MIME_TYPE as the latter exist only in PHP 5.3+
*/
$bad_mime = array('application/octet-stream', 'application/x-character-device');
if (!$mime_type && function_exists('finfo_open')) {
$fileInfo = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($fileInfo, $file_name);
$mime_type = explode(';', $mime_type);
$mime_type = trim($mime_type[0]);
$finfo_close = ($fileInfo);
}
if ((!$mime_type || in_array($mime_type, $bad_mime)) && function_exists('mime_content_type')) {
$mime_type = mime_content_type($file_name);
}
if (!$mime_type || in_array($mime_type, $bad_mime)) {
$cmd = 'file -ib '.escapeshellarg($file_name).' 2> /dev/null';
$mime_type = exec($cmd);
$mime_type = explode(';', $mime_type);
$mime_type = trim($mime_type[0]);
}
if ((!$mime_type || in_array($mime_type, $bad_mime)) && $upload_type) {
$mime_type = $upload_type;
}
return $mime_type;
}