Index: file.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/file.inc,v retrieving revision 1.106 diff -u -r1.106 file.inc --- file.inc 2 Oct 2007 16:15:56 -0000 1.106 +++ file.inc 4 Oct 2007 16:58:12 -0000 @@ -535,7 +535,11 @@ $file->filepath .= '.txt'; $file->filename .= '.txt'; } - + // Fixing wrong Mimetype sent by Browser + else { + $file->filemime = file_get_mimetype($_FILES['files']['tmp_name'][$source]); + } + // Create temporary name/path for newly uploaded files. if (!$dest) { $dest = file_destination(file_create_path($file->filename), FILE_EXISTS_RENAME); @@ -974,3 +978,53 @@ } return $max_size; } + +/** + * Retrive file MimeType using fallback method: + * 1. use PECL fileinfo() if avaiable + * 2. use PHP mime_content_type() if avaiable + * 3. use browser setted mimetype if setted +*/ +function file_get_mimetype($filepath, $default = 'application/download') { + $used = ''; + $filemime = NULL; + + // Retriving MAGIC env variable: needed for PECL + // See here: http://pecl.php.net/bugs/bug.php?id=11000 + // ------------------------------------ MUST fix that!!! + if(getenv('MAGIC') === FALSE) { + // windows path???? + if (substr(PHP_OS, 0, 3) == 'WIN') { + // See here: http://pecl.php.net/bugs/bug.php?id=10565 + $path = ini_get('extension_dir'); + $path = realpath($path.'/../'); + putenv('MAGIC='. $path .'extras/magic'); + } + // Linux path??? + else { + putenv('MAGIC=/usr/share/file/magic'); + } + } + + // Using PECL fileinfo + if(function_exists('finfo_open') && $finfo = new finfo(FILEINFO_MIME)) { + // realpath() needed for bug in FileInfo->file() function + // See here: http://pecl.php.net/bugs/bug.php?id=10259 + $filemime = $finfo->file(realpath($filepath)); + unset($finfo); + $used = 'PECL-FileInfo'; + } + // Using mime_content_type() + elseif(function_exists('mime_content_type')) { + // seems that with PHP5 mime_contet_type doesn't work with shipped magic.mime!! + $filemime = mime_content_type($filepath); + $used = 'mime_content_type()'; + } + // Using, if setted, browser setted mimetype + if(empty($filemime)) { + $used .= ' - EMPTY'; + $filemime = $default; + } + watchdog('file',t('Mimetype setted to: %destination using %function, MAGIC=' .getenv('MAGIC'), array('%destination' => $filemime, '%function' => $used))); + return $filemime; +}