How do you loop through $_FILES array?

How do you loop through $_FILES array?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Structure of $_FILES and Why Your Loop Isn't Running

When you define multiple file inputs sharing the same name, like image[], you are technically creating a multidimensional array structure within your superglobal. It is easy to assume that $_FILES['image'] behaves like a simple list of uploaded files, but the underlying data model includes metadata for every input field regardless of whether a file was actually selected.

This distinction explains the behavior you observed with count($_FILES['image']). Even if your HTML form contains no <form> submission or the user uploads nothing, PHP still populates the keys associated with those four inputs. The array structure contains an entry for each input field defined in the source. When you call count() on this multidimensional array, PHP calculates the number of top-level keys (the four inputs) plus potentially one key representing the "no file chosen" state or a default entry depending on how the browser serializes the empty form data. In many practical scenarios with multiple empty inputs, the count often reflects the total number of slots available rather than just the files present. This is why you see a value of 5 when expecting 0; the array structure itself holds space for the four defined inputs, and the extra element usually corresponds to the implicit default or how the browser handles the name[] attribute in an empty submission.

Attempting to loop through this data with a standard foreach loop often fails because you are likely iterating over the top-level metadata instead of the actual file entries. The structure of $_FILES['image'] is not a flat list of files; it is an associative array where each key corresponds to one specific input field.

// This loop iterates over the four input slots, not the files
foreach ($_FILES['image'] as $files) {
    echo $files['name']; 
}

In this snippet, $files represents the metadata array for a single input slot (containing keys like name, type, tmp_name, error, and size). If no file is selected for that specific input, the name key might be empty or missing, but the loop still executes once per input field. To process only the files that were actually uploaded, you must filter out entries where the error code indicates no file was chosen.

The correct approach involves checking the error index within each slot. A value of UPLOAD_ERR_NO_FILE (which is 4) signifies that an input field exists but the user did not select a file. By filtering for this specific error code, you can isolate the actual uploads.

foreach ($_FILES['image'] as $index => $files) {
    // Check if the error code indicates no file was uploaded
    if ($files['error'] === UPLOAD_ERR_NO_FILE) {
        continue; 
    }

    // Now you can safely process the actual file
    $fileName = basename($files['name']);
    $tmpName = $files['tmp_name'];
    
    // Perform your validation logic here (format, size)
    // Then rename and move the file as needed
}

This pattern ensures that your loop only processes valid uploads while ignoring empty slots. The index variable in the foreach loop is crucial here; it allows you to track which input slot corresponds to which file if you need to maintain a specific order or mapping (e.g., Main Photo vs. Side Photo 1).

If you are building this functionality within a Laravel application, consider leveraging the Illuminate\Http\UploadedFile class for cleaner validation and moving operations. You can create a collection of uploaded files from the raw array, filter by error code, and apply validation rules before moving the files to your storage directory. This abstraction handles many edge cases, such as ensuring unique filenames during the rename process, which is often necessary when processing multiple side photos with the same base name.

Our network