Index: includes/file.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/file.inc,v
retrieving revision 1.124
diff -u -p -r1.124 file.inc
--- includes/file.inc	23 Apr 2008 18:17:41 -0000	1.124
+++ includes/file.inc	22 May 2008 18:36:04 -0000
@@ -535,6 +535,10 @@ function file_save_upload($source, $vali
       $file->filepath .= '.txt';
       $file->filename .= '.txt';
     }
+    // Fixing wrong Mimetype sent by Browser
+    else {
+      $file->filemime = file_get_mimetype($_FILES['files']['tmp_name'][$source]);
+    }
 
     // If the destination is not provided, or is not writable, then use the
     // temporary directory.
@@ -978,6 +982,57 @@ function file_upload_max_size() {
   }
   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;
+
+  // Retrieving MAGIC env variable: needed for PECL
+  // See here: http://pecl.php.net/bugs/bug.php?id=11000
+  // ------------------------------------  MUST fix that!!!
+  if (getenv('MAGIC') === FALSE) {
+    // Is it a 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');
+    }
+    // ... or a Unix 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 PHP5 mime_content_type doesn't work with shipped magic.mime!!
+    $filemime = mime_content_type($filepath);
+    $used = 'mime_content_type()';
+  }
+
+  // Using mimetype from browser, if not set above
+  if (empty($filemime)) {
+    $used .= t("browser's HTTP request");
+    $filemime = $default;
+  }
+  watchdog('file', 'MIME-type set to %mimetype using %function, MAGIC=%magic', array('%mimetype' => $filemime, '%function' => $used, '%magic' => getenv('MAGIC')));
+  return $filemime;
+}
 
 /**
  * @} End of "defgroup file".
