From 78423c1bd0a6b0379902a50e0680f4a29bc207a1 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Fri, 18 May 2012 09:22:02 +1200 Subject: [PATCH] BUGFIX Ensure HTTP::get_mime_type() checks the full path in file_exists() before using the finfo class --- control/HTTP.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/control/HTTP.php b/control/HTTP.php index 924d2f95c..7b4c9fbad 100644 --- a/control/HTTP.php +++ b/control/HTTP.php @@ -196,7 +196,10 @@ class HTTP { } /** - * Get mime type based on file extension. + * Get the MIME type based on a file's extension. + * + * If the finfo class exists in PHP, and the file actually exists, then use that + * extension, otherwise fallback to a list of commonly known MIME types. * * @uses finfo * @param string $filename Relative path to filename from project root, e.g. "mysite/tests/file.csv" @@ -204,20 +207,23 @@ class HTTP { */ public static function get_mime_type($filename) { // If the finfo module is compiled into PHP, use it. - if(class_exists('finfo') && file_exists($filename)) { + $path = BASE_PATH . DIRECTORY_SEPARATOR . $filename; + if(class_exists('finfo') && file_exists($path)) { $finfo = new finfo(FILEINFO_MIME_TYPE); - return $finfo->file(BASE_PATH . DIRECTORY_SEPARATOR . $filename); + return $finfo->file($path); } - + // Fallback to use the list from the HTTP.yml configuration and rely on the file extension // to get the file mime-type $ext = File::get_file_extension($filename); // Get the mime-types $mimeTypes = Config::inst()->get('HTTP', 'MimeTypes'); + // The mime type doesn't exist if(!isset($mimeTypes[$ext])) { return 'application/unknown'; } + return $mimeTypes[$ext]; }