Created
September 30, 2011 09:07
-
-
Save evaisse/1253179 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* get file mimetype | |
* | |
* Tries to get mime data of the file. | |
* | |
* @param str $filepath | |
* @return str mime-type of the given file, or false if nothing found | |
*/ | |
function get_file_mimetype( $filepath ) | |
{ | |
if( !file_exists($filepath) ) | |
{ | |
return false; | |
} | |
if( function_exists("mime_content_type") ) | |
{ | |
// if mime_content_type exists use it. | |
$m = mime_content_type($filepath); | |
} | |
else if( function_exists('finfo_open') ) | |
{ | |
// if php5.3 or Pecl installed use it | |
$finfo = finfo_open(FILEINFO_MIME); | |
$m = finfo_file($finfo, $filepath); | |
finfo_close($finfo); | |
} | |
else | |
{ | |
// if nothing left try shell | |
if( strstr($_SERVER['SERVER_SOFTWARE'], "Windows") ) | |
{ | |
# Nothing to do on windows | |
# anyway, blank mime display most files correctly especially images. | |
return false; | |
} | |
elseif( strstr($_SERVER['SERVER_SOFTWARE'], "Macintosh") ) | |
{ | |
# Correct output on macs | |
$m = trim( exec('file -b --mime '.escapeshellarg($filepath)) ); | |
} | |
else | |
{ | |
# Regular unix systems | |
$m = trim( exec('file -bi '.escapeshellarg($filepath)) ); | |
} | |
} | |
return $m; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment