/** * Attempt to get the mime type from a file. This method is horribly * unreliable, due to PHP being horribly unreliable when it comes to * determining the mime type of a file. * * $mime = File::mime($file); * * @param string $filepath file name or path * @param string $realName The real fileName of the file (without the path) * @return string mime type on success * @return FALSE on failure */ public static function mime($fileName, $realName) { // Get the complete path to the file $fileName = realpath($fileName); // Get the extension from the fileName $extension = strtolower(pathinfo($realName, PATHINFO_EXTENSION)); if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension)) { // Use getimagesize() to find the mime type on images $file = getimagesize($fileName); if (isset($file['mime'])) { return $file['mime']; } } // First lets try finfo if (class_exists('finfo')) { if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) { return $info->file($fileName); } elseif ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME, 'magic')) { return $info->file($fileName); } } // No finfo, so lets try mime_content_type if (function_exists('mime_content_type')) { $mimetype = mime_content_type($fileName); if ($mimetype) { return $mimetype; } } // Nothing else has worked,lets try the mimetypes global array if (!empty($extension)) { return File::mimeByExt($extension); } // Unable to find the mime-type return false; }
/** * Attempt to get the mime type from a file. This method is horribly * unreliable, due to PHP being horribly unreliable when it comes to * determining the mime type of a file. * * $mime = File::mime($file); * * @param string $filename file name or path * @return string mime type on success * @return FALSE on failure */ public static function mime($filename) { // Get the complete path to the file $filename = realpath($filename); // Get the extension from the filename $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension)) { // Use getimagesize() to find the mime type on images $file = getimagesize($filename); if (isset($file['mime'])) { return $file['mime']; } } if (class_exists('finfo', FALSE)) { if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) { return $info->file($filename); } } if (ini_get('mime_magic.magicfile') and function_exists('mime_content_type')) { // The mime_content_type function is only useful with a magic file return mime_content_type($filename); } if (!empty($extension)) { return File::mimeByExt($extension); } // Unable to find the mime-type return FALSE; }