diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index a2a5a80..fe10e6b 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -255,20 +255,17 @@ function file_ajax_upload() {
     return array('#type' => 'ajax', '#commands' => $commands);
   }
 
-  // Get the current element and count the number of files.
-  $current_element = $form;
-  foreach ($form_parents as $parent) {
-    $current_element = $current_element[$parent];
-  }
-  $current_file_count = isset($current_element['#file_upload_delta']) ? $current_element['#file_upload_delta'] : 0;
-
   // Process user input. $form and $form_state are modified in the process.
   drupal_process_form($form['#form_id'], $form, $form_state);
 
-  // Retrieve the element to be rendered.
+  // Get the current element and count the number of files and retrieve the
+  // element to be rendered.
+  $current_element = $form;
   foreach ($form_parents as $parent) {
+    $current_element = $current_element[$parent];
     $form = $form[$parent];
   }
+  $current_file_count = isset($current_element['#file_upload_delta']) ? $current_element['#file_upload_delta'] : 0;
 
   // Add the special Ajax class if a new file was added.
   if (isset($form['#file_upload_delta']) && $current_file_count < $form['#file_upload_delta']) {
