--- file.head	2005-11-04 15:48:12.000000000 -0500
+++ file.inc	2005-11-04 16:27:03.000000000 -0500
@@ -57,7 +57,7 @@
     return $dest;
   }
   // check if the destination is instead inside the Drupal temporary files directory.
-  else if (file_check_location($dest, variable_get('file_directory_temp', ini_get('upload_tmp_dir')))) {
+  else if (file_check_location($dest, variable_get('file_directory_temp', file_tmp_dir()))) {
     return $dest;
   }
   // Not found, try again with prefixed dirctory path.
@@ -367,7 +367,7 @@
   // Make sure $source exists in $_FILES.
   if ($file = file_check_upload($source)) {
     if (!$dest) {
-      $dest = variable_get('file_directory_temp', ini_get('upload_tmp_dir'));
+      $dest = variable_get('file_directory_temp', file_tmp_dir());
       $temporary = 1;
       if (is_file($file->filepath)) {
         // If this file was uploaded by this user before replace the temporary copy.
@@ -425,7 +425,7 @@
     return 0;
   }
 
-  $temp = variable_get('file_directory_temp', ini_get('upload_tmp_dir'));
+  $temp = variable_get('file_directory_temp', file_tmp_dir());
   $file = tempnam($temp, 'file');
   if (!$fp = fopen($file, 'wb')) {
     drupal_set_message(t('The file could not be created.'), 'error');
@@ -551,4 +551,35 @@
   return $files;
 }
 
+/**
+ * Determine the best default temp directory.
+ */
+function file_tmp_dir() {
+  static $tmp_dir;
+
+  // Has PHP been set with an upload_tmp?
+  if (ini_get('upload_tmp_dir') !== NULL) {
+    $tmp_dir = ini_get('upload_tmp_dir');
+  }
+
+  // Determine based on operating system.
+  elseif (substr(PHP_OS, 0, 3) == 'WIN') {
+    $tmp_dir = 'files/tmp'; // last resort.
+
+    // Windows env var TEMP may not exist, so try other common locations too.
+    $directories = array($_GLOBALS['TEMP'], 'c:\\windows\\temp', 'c:\\winnt\\temp');
+    foreach ($directories as $directory) {
+      if (is_dir($directory)) {
+        $tmp_dir = $directory;
+      }
+    }
+  }
+
+  // Linux.
+  else {
+    $tmp_dir = '/tmp';
+  }
+
+  return $tmp_dir;
+}
 
--- modules/system.head	2005-11-04 15:54:22.000000000 -0500
+++ modules/system.module	2005-11-04 15:54:55.000000000 -0500
@@ -325,7 +325,7 @@
     '#description' => t('A file system path where the files will be stored. This directory has to exist and be writable by Drupal. If the download method is set to public this directory has to be relative to Drupal installation directory, and be accessible over the web. When download method is set to private this directory should not be accessible over the web. Changing this location after the site has been in use will cause problems so only change this setting on an existing site if you know what you are doing.')
   );
 
-  $directory_temp = variable_get('file_directory_temp', ini_get('upload_tmp_dir'));
+  $directory_temp = variable_get('file_directory_temp', file_tmp_dir());
   file_check_directory($directory_temp, FILE_CREATE_DIRECTORY, 'file_directory_temp');
 
   $form['files']['file_directory_temp'] = array(
