Index: includes/file.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/file.inc,v
retrieving revision 1.191
diff -u -p -r1.191 file.inc
--- includes/file.inc	28 Sep 2009 22:22:53 -0000	1.191
+++ includes/file.inc	30 Sep 2009 18:28:17 -0000
@@ -1886,7 +1886,7 @@ function drupal_mkdir($uri, $mode = NULL
  * @ingroup php_wrappers
  */
 function drupal_tempnam($directory, $prefix) {
- 	$scheme = file_uri_scheme($directory);
+  $scheme = file_uri_scheme($directory);
 
   if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
     $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
Index: includes/stream_wrappers.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/stream_wrappers.inc,v
retrieving revision 1.6
diff -u -p -r1.6 stream_wrappers.inc
--- includes/stream_wrappers.inc	31 Aug 2009 05:47:33 -0000	1.6
+++ includes/stream_wrappers.inc	30 Sep 2009 18:39:09 -0000
@@ -625,6 +625,26 @@ class DrupalTemporaryStreamWrapper exten
    * Implements abstract public function getDirectoryPath()
    */
   public function getDirectoryPath() {
+    // Since PHP 5.2.1, PHP exposes the system's temporary directory.
+    if (function_exists('sys_get_temp_dir')) {
+      return sys_get_temp_dir();
+    }
+
+    // @todo Remove this section when Drupal requires at least PHP 5.2.1.
+    if ($dir = variable_get('file_temporary_path', NULL)) {
+      return $dir;
+    }
+
+    // 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_temporary_path', $dir);
+    }
+
+    // Otherwise, default to private file storage.
     return variable_get('file_temporary_path', conf_path() . '/private/temp');
   }
 
Index: modules/simpletest/tests/file.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/file.test,v
retrieving revision 1.44
diff -u -p -r1.44 file.test
--- modules/simpletest/tests/file.test	20 Sep 2009 17:40:41 -0000	1.44
+++ modules/simpletest/tests/file.test	30 Sep 2009 18:34:06 -0000
@@ -745,7 +745,7 @@ class FileDirectoryTest extends FileTest
    */
   function testFileDirectoryTemp() {
     // Temporary directory handling.
-    variable_set('file_directory_temp', NULL);
+    variable_set('file_temporary_path', NULL);
     $temp = file_directory_path('temporary');
     $this->assertTrue(!is_null($temp), t('Properly set and retrieved temp directory %directory.', array('%directory' => $temp)), 'File');
   }
