Index: node_gallery.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_gallery/node_gallery.pages.inc,v
retrieving revision 1.11.4.29
diff -u -r1.11.4.29 node_gallery.pages.inc
--- node_gallery.pages.inc	8 May 2010 14:02:30 -0000	1.11.4.29
+++ node_gallery.pages.inc	11 May 2010 01:05:30 -0000
@@ -144,9 +144,11 @@
   // Handle new uploads, and merge tmp files into node-files.
   node_gallery_upload_images($cached_form, $form_state);
   
-  //if upload failed.
-  if (drupal_get_messages('error', FALSE)) {
-    print drupal_to_js(array('status' => TRUE, 'data' => theme('status_messages') . drupal_get_form('node_gallery_upload_form')));
+  // Exit if no images were successfully uploaded
+  if (empty($form_state['values']['files'])) {
+    if (drupal_get_messages('error', FALSE)) {
+      print drupal_to_js(array('status' => TRUE, 'data' => theme('status_messages') . drupal_get_form('node_gallery_upload_form')));
+    }
     exit;
   }
   
@@ -212,9 +214,11 @@
     'file_validate_extensions' => array($limits['extensions']),
     'file_validate_image_resolution' => array($limits['resolution']),
     'file_validate_size' => array($limits['file_size'], $limits['user_size']),
-    'file_validate_number' => array($limits['max_number']),
   );
 
+  // We need to keep track of the number of files being currently updated so
+  // that we can avoid http://drupal.org/node/601186#comment-2951536
+  $temp_file_count = 0;
   // Save new file uploads.
   if (($user->uid != 1 || user_access('upload files'))) {
     $directory = node_gallery_check_directory($form['#node']);
@@ -228,6 +232,8 @@
           $_FILES['files']['name'][$id] = 'file-'. $_FILES['files']['name'][$id];
         }
         
+        $temp_file_count++;
+        $validators['file_validate_number'] = array($limits['max_number'], $temp_file_count);
         $file = file_save_upload($id, $validators, $directory);
 
         if ($file != 0) {
@@ -670,12 +676,15 @@
   return $form;
 }
 
-function file_validate_number($file, $max_number) {
+function file_validate_number($file, $max_number, $temp_count) {
   global $user;
   $errors = array();
   
   if ($user->uid != 1) {
-    $current_number = db_result(db_query("SELECT COUNT(*) FROM {files} WHERE uid = %d", $user->uid));
+    $current_number = db_result(db_query("SELECT COUNT(*) FROM {files} WHERE uid = %d and status = %d", $user->uid, FILE_STATUS_PERMANENT));
+    if ($temp_count > 0) {
+      $current_number += $temp_count;
+    }
     if ($current_number >= $max_number && $max_number != '0') {
       $errors[] = t("You can only upload a maximum of %num images.", array('%num' => $max_number));
     }
