Came up with a "workable" solution at last. Problem is that it's never ultimately working because even if you are able to not have to go to the level of reading "mime.types" and "guessing" the file's MIME type, any other method you use to try to figure out a file's MIME type is not 100% guaranteed that you are getting "as advertised" as MIME types can be spoofed.
/*-----------------------------------------------------------------------------------------------------------------------
Note: Certain variations of PHP may not have these functions installed within php.ini if this is
the case these functions will kick in instead
------------------------------------------------------------------------------------------------------------------------*/
if (!function_exists('mime_content_type')) {
/**
* Note: Certain variations of PHP may not have these functions installed within php.ini if this is the case these functions will kick in instead
*
* @access public
* @param mixed $file
* @return mixed UNIX
* @link [url]http://us3.php.net/manual/en/function.mime-content-type.php#50756[/url]
* @see link regarding usage of mime_content_type in Windows platform
* @see array_remove_element_at
*/
function mime_content_type($file) {
global $mimeTypeFilePath, $windowsMagicMimePath;
if (!$_ENV['windir']) $mime = exec('file -bi ' . escapeshellarg($file) . ' 2>&1'); // STOP HERE, WILL WORK IN UNIX
if ((!$_ENV['windir'] && !preg_match('/^[a-zA-Z0-9\-_\.]+\/[a-zA-Z0-9\-_\.]+$/i', trim($mime))) || $_ENV['windir']) {
$array = @getimagesize($file); // OBTAIN INFORMATION - WILL PARTITION IF FILE IS IMAGE OR NOT
if (is_array($array) && @sizeof($array) > 0) return $array['mime']; // SHOULD EITHER RETURN A MIME TYPE OR NOTHING
/*---------------------------------------------------------------------------------------------------------------------------------------------------------------
New 5/3/2005: Last Resort: Pull up the entire contents of the 'mime.types' file found in $mimeTypesFilesName, parse into array,
and check against the extension of the file. ABSOLUTELY NOT RECOMMENDED due to potentially severe spoofs as it will only
check against the extension of the file and "guess" what MIME type it should have based upon the extension and not actually
read the actual MIME type due to lack of MIME-related support on the server (since it got to this point)
----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
$fileID = @fopen($mimeTypeFilePath, 'rb'); // READ AS BINARY JUST IN CASE
if ($fileID) {
$contents = @fread($fileID, filesize($mimeTypeFilePath));
@fclose($fileID);
$mimeArray = @explode("\n", $contents);
if (!$mimeArray[@sizeof($mimeArray) - 1]) $junk = @array_pop($mimeArray); // POP OFF NULL LAST VALUE OF $mimeArray IF IT IS NULL
// REMOVE FIRST FEW ROWS OF $mimeArray AS THEY ARE EMBEDDED COMMENTS FOUND WITHIN mime.types FILE
while ((is_numeric(strpos(trim($mimeArray[0]), '#')) && strpos(trim($mimeArray[0]), '#') == 0) || !$mimeArray[0]) $junk = @array_shift($mimeArray);
@reset($mimeArray);
for ($i = 0; $i < @sizeof($mimeArray); $i++) {
unset($array); // REMEMBER TO REMOVE EACH TIME TO ENSURE FRESH ROW
if (preg_match('/^[^\s\t]+[\s\t]+([^\s\t]+[\s\t]*)+[^\s\t]*$/i', $mimeArray[$i])) {
$array = preg_split('/[\s\t]+/i', $mimeArray[$i]);
for ($j = 1; $j < @sizeof($array); $j++) $newMimeArray[$array[$j]] = $array[0];
}
}
return $newMimeArray[substr($file, strrpos($file, '.') + 1, strlen($file))];
}
} else {
return $mime;
}
}
}
This works unless you're using:
$mime = mime_content_type($_FILES[$fileName]['tmp_name']);
Because if MIME-related functionality is removed and mime_content_type() is forced to "guess" the MIME type via mime.types, the dependency will be the inputted file's extension, which $_FILES[$fileName]['tmp_name'] will not have. Thus, you have to do this:
$mime = mime_content_type($_FILES[$fileName]['tmp_name']); // IAPW: Should get the MIME type
if (!$mime) $mime = mime_content_type($_FILES[$fileName]['name']); // This time you should get the "guessed" MIME type based on the extension
Phil