Index: file.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/file.inc,v
retrieving revision 1.90.2.1
diff -u -r1.90.2.1 file.inc
--- file.inc	31 May 2007 05:48:58 -0000	1.90.2.1
+++ file.inc	4 Oct 2007 16:49:48 -0000
@@ -234,7 +234,10 @@
       $file->filepath .= '.txt';
       $file->filename .= '.txt';
     }
-
+    // Fixing wrong Mimetype sent by Browser
+    else {
+      $file->filemime = file_get_mimetype($_FILES["files"]["tmp_name"][$source]);
+    }
     // Move uploaded files from php's upload_tmp_dir to Drupal's file temp.
     // This overcomes open_basedir restrictions for future file operations.
     if (!move_uploaded_file($_FILES["files"]["tmp_name"][$source], $file->filepath)) {
@@ -732,3 +735,54 @@
   }
   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;
+}
+
