=== modified file 'includes/file.inc'
--- includes/file.inc	2008-12-04 11:09:33 +0000
+++ includes/file.inc	2008-12-12 15:56:02 +0000
@@ -1321,37 +1321,28 @@ function file_scan_directory($dir, $mask
  *   A string containing a temp directory.
  */
 function file_directory_temp() {
-  $temporary_directory = variable_get('file_directory_temp', NULL);
-
-  if (is_null($temporary_directory)) {
-    $directories = array();
-
-    // Has PHP been set with an upload_tmp_dir?
-    if (ini_get('upload_tmp_dir')) {
-      $directories[] = ini_get('upload_tmp_dir');
-    }
-
-    // Operating system specific dirs.
-    if (substr(PHP_OS, 0, 3) == 'WIN') {
-      $directories[] = 'c:/windows/temp';
-      $directories[] = 'c:/winnt/temp';
-    }
-    else {
-      $directories[] = '/tmp';
-    }
-
-    foreach ($directories as $directory) {
-      if (!$temporary_directory && is_dir($directory)) {
-        $temporary_directory = $directory;
-      }
+  if (function_exists('sys_get_temp_dir')) {
+    return sys_get_temp_dir();
+  }
+  // @TODO: remove this section once Drupal requires at least PHP 5.2.1
+  if ($dir = variable_get('file_directory_temp', NULL)) {
+    return $dir;
+  }
+  foreach (array('TMP', 'TMPDIR', 'TEMP') as $key) {
+    if (isset($_ENV[$key])) {
+      variable_set('file_directory_temp', $_ENV[$key]);
+      return $_ENV[$key];
     }
-
-    // if a directory has been found, use it, otherwise default to 'files/tmp'
-    $temporary_directory = $temporary_directory ? $temporary_directory : file_directory_path() . '/tmp';
-    variable_set('file_directory_temp', $temporary_directory);
   }
-
-  return $temporary_directory;
+  // tempnam falls back to the system directory if the one specified is not
+  // found and this directory is very unlikely to exist.
+  $tempfile = tempnam(uniqid(mtrand(), TRUE), '');
+  if (file_exists($tempfile)) {
+    $dir = realpath(dirname($tempfile));
+    unlink($tempfile);
+    variable_set('file_directory_temp', $dir);
+    return $dir;
+  }
 }
 
 /**

