=== modified file 'modules/upload/upload.module'
--- modules/upload/upload.module	2007-08-16 12:30:24 +0000
+++ modules/upload/upload.module	2007-08-30 05:18:55 +0000
@@ -165,17 +165,9 @@ function upload_file_download($file) {
  * @param $node
  *   A node object to associate with uploaded files.
  */
-function _upload_prepare(&$node) {
+function upload_node_form_submit($form, &$form_state) {
   global $user;
 
-  // Initialize _SESSION['upload_files'] if no post occurred.
-  // This clears the variable from old forms and makes sure it
-  // is an array to prevent notices and errors in other parts
-  // of upload.module.
-  if (!$_POST) {
-    $_SESSION['upload_files'] = array();
-  }
-
   // $_SESSION['upload_current_file'] tracks the fid of the file submitted this page request.
   // form_builder sets the value of file->list to 0 for checkboxes added to a form after
   // it has been submitted. Since unchecked checkboxes have no return value and do not
@@ -199,9 +191,9 @@ function _upload_prepare(&$node) {
   }
 
   // attach session files to node.
-  if (count($_SESSION['upload_files'])) {
+  if (!empty($_SESSION['upload_files'])) {
     foreach($_SESSION['upload_files'] as $fid => $file) {
-      $node->files[$fid] = $file;
+      $form_state['values']['files'][$fid] = $file;
     }
   }
 }
@@ -257,6 +249,7 @@ function upload_form_alter(&$form, $form
         $form['#attributes']['enctype'] = 'multipart/form-data';
       }
     }
+    $form['#submit'][] = 'upload_node_form_submit';
   }
 }
 
@@ -274,10 +267,6 @@ function upload_nodeapi(&$node, $op, $te
       }
       break;
 
-    case 'prepare':
-      _upload_prepare($node);
-      break;
-
     case 'view':
       if (isset($node->files) && user_access('view uploaded files')) {
         // Add the attachments list to node body with a heavy
@@ -293,6 +282,16 @@ function upload_nodeapi(&$node, $op, $te
       }
       break;
 
+    case 'prepare':
+      // Initialize _SESSION['upload_files'] if no post occurred.
+      // This clears the variable from old forms and makes sure it
+      // is an array to prevent notices and errors in other parts
+      // of upload.module.
+      if (!$_POST) {
+        $_SESSION['upload_files'] = array();
+      }
+      break;
+
     case 'insert':
     case 'update':
       if (user_access('upload files')) {
@@ -390,7 +389,7 @@ function upload_save(&$node) {
 
     // Remove file. Process removals first since no further processing
     // will be required.
-    if ($file->remove) {
+    if (!empty($file->remove)) {
       db_query('DELETE FROM {upload} WHERE fid = %d AND vid = %d', $fid, $node->vid);
       // Remove it from the session in the case of new uploads,
       // that you want to disassociate before node submission.
@@ -400,7 +399,7 @@ function upload_save(&$node) {
     }
 
     // Create a new revision, or associate a new file needed.
-    if (!empty($node->old_vid) || array_key_exists($fid, $_SESSION['upload_files'])) {
+    if (!empty($node->old_vid) || isset($_SESSION['upload_files'][$fid])) {
       db_query("INSERT INTO {upload} (fid, nid, vid, list, description) VALUES (%d, %d, %d, %d, '%s')", $file->fid, $node->nid, $node->vid, $file->list, $file->description);
       file_set_status($file, FILE_STATUS_PERMANENT);
     }
@@ -550,13 +549,13 @@ function upload_load($node) {
 function upload_js() {
   // We only do the upload.module part of the node validation process.
   $node = (object)$_POST;
-  $files = isset($_POST['files']) ? $_POST['files'] : array();
+  $form_state = array();
+  // Handle new uploads, and merge tmp files into node-files.
+  upload_node_form_submit(array(), $form_state);
+  $node->files = array_merge(isset($form_state['values']['files']) ? $form_state['values']['files'] : array(), upload_load($node));
 
-  // Load existing node files.
-  $node->files = upload_load($node);
+  $files = isset($_POST['files']) ? $_POST['files'] : array();
 
-  // Handle new uploads, and merge tmp files into node-files.
-  _upload_prepare($node);
 
   $form = _upload_form($node);
   $form += array(

