Index: includes/file.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/file.inc,v
retrieving revision 1.62
diff -u -r1.62 file.inc
--- includes/file.inc	18 Jan 2006 19:18:30 -0000	1.62
+++ includes/file.inc	24 Feb 2006 21:20:31 -0000
@@ -137,15 +138,32 @@
  * @param $source
  */
 function file_check_upload($source) {
+  static $upload_cache = array();
+
   if (is_object($source)) {
     if (is_file($source->filepath)) {
       return $source;
     }
+    return false;
   }
-  elseif ($_FILES["edit"]["name"][$source] && is_uploaded_file($_FILES["edit"]["tmp_name"][$source])) {
+
+  if (isset($upload_cache[$source])) return $upload_cache[$source];
+    
+  if ($_FILES["edit"]["name"][$source] && is_uploaded_file($_FILES["edit"]["tmp_name"][$source])) {
     $file = new StdClass();
     $file->filename = trim(basename($_FILES["edit"]["name"][$source]), '.');
-    $file->filepath = $_FILES["edit"]["tmp_name"][$source];
+
+    //create temporary name/path for newly uploaded files.
+    $file->filepath = tempnam(file_directory_temp(), 'tmp_');
+
+    //move uploaded files from php's upload_tmp_dir to Drupal's file temp, for preview and pre-submit
+    //operations.  overcomes open_basedir restriction as per node/5961
+    
+    if (!move_uploaded_file($_FILES["edit"]["tmp_name"][$source], $file->filepath)) {
+      drupal_set_message(t('File upload error. Could not move uploaded file.'));
+        watchdog('file', t('Upload Error. Could not move uploaded file(%file) to destination(%destination).', array('%file' => theme('placeholder', $_FILES["edit"]["tmp_name"][$source]), '%destination' => theme('placeholder', $file->filepath))));
+      return false;
+    }
 
     if (function_exists('mime_content_type')) {
       $file->filemime = mime_content_type($file->filepath);
@@ -166,6 +184,7 @@
     $file->error = $_FILES["edit"]["error"][$source];
     $file->filesize = $_FILES["edit"]["size"][$source];
     $file->source = $source;
+    $upload_cache[$source] = $file;
     return $file;
   }
   else {
Index: modules/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system.module,v
retrieving revision 1.291
diff -u -r1.291 system.module
--- modules/system.module	21 Feb 2006 18:46:54 -0000	1.291
+++ modules/system.module	24 Feb 2006 21:20:32 -0000
@@ -348,6 +348,13 @@
     '#after_build' => 'system_check_directory',
   );
 
+  $form['files']['file_gc_period'] = array(
+    '#type' => 'textfield',
+    '#title' => t('File GC period'),
+    '#default_value' => variable_get('file_gc_period', 300),
+    '#description' => t('Seconds a temporary file is allowed to go without modification before being garabage collected. (removed)'),
+  );
+
   $form['files']['file_downloads'] = array(
     '#type' => 'radios', '#title' => t('Download method'), '#default_value' => variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC),
     '#options' => array(FILE_DOWNLOADS_PUBLIC => t('Public - files are available using http directly.'), FILE_DOWNLOADS_PRIVATE => t('Private - files are transferred by Drupal.')),
@@ -509,9 +516,34 @@
   }
 
   $form['settings'] = array('#type' => 'item', '#value' => $status);
+
+  
   return $form;
 }
 
+
+/**
+ * implementation of hook cron for system level events.
+ */
+ 
+function system_cron() {
+  //perform garbage collection of drupal's temporary files.
+  $now = time();
+  $handle = opendir(file_directory_temp());
+  while (false !== ($file = readdir($handle))) {
+    if ($file == '..' or $file == '.') continue;
+
+    $file = file_directory_temp() .'/'. $file;
+    $lastmodified = filemtime($file);
+    $since = $now - $lastmodified;
+   
+    if ($since > variable_get('file_gc_period', 300)) {
+      unlink($file);
+    }
+  }
+}
+
+
 /**
  * Retrieves the current status of an array of files in the system table.
  */
