This function is used to get the mime type of any given file. The intended purpose was to have it working within any of our servers that vary from the latest PHP version down to PHP 4.
/**
* Get the mime type for any given file.
*
* @param str $filename Path to the file including the filename
*
* @return mixed bol|arr Either false or the array provided by the PHP function
*/
function getMimeType($filename) {
$mimetype = false;
if (function_exists('finfo_fopen')) {
$mimetype = finfo_fopen($filename);
} elseif (function_exists('getimagesize')) {
$mimetype = getimagesize($filename);
} elseif (function_exists('exif_imagetype')) {
$mimetype = exif_imagetype($filename);
} elseif (function_exists('mime_content_type')) {
$mimetype = mime_content_type($filename);
}
return $mimetype;
}
While the function is already tested on different PHP versions, I'm trying to ascertain if the methodology implemented can be improved, thus leading to code reduction and preventing any redundant verifications.
My question is:
Can this function receive any type of improvement?
@return mixed bol|arr Either false or the array provided by the PHP functionThat's a major flaw. How is the consumer of this function to know what to expect? Also, the documentation is a lie. exif_imagetype returns an integer, not an array. Similarly, finfo_fopen returns a resource, and you're calling it incorrectly. It's basically impossible to use this function unless you repeat the if-elseif tree in the consuming code to know how to interpret the return value. – Corbin Sep 21 '12 at 4:54