How can I select and upload multiple files with HTML and PHP, using HTTP POST?

How can I select and upload multiple files with HTML and PHP, using HTTP POST?

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Multiple File Uploads in HTML and PHP

You cannot select multiple files with a standard <input type="file"> element without modifying its attributes. The default behavior is to accept exactly one file, which aligns with many single-file upload workflows but fails when you need to batch process images or documents. To enable the selection of a series of files in a single action, you must include the multiple attribute on the input tag.

<input type="file" name="files[]" multiple>

The critical detail here is the double quotes around files[]. When using PHP's $_FILES superglobal to process uploads, the naming convention determines how the data is accessed. If you name the input files[], PHP will treat every selected file as an element in an associative array within $_FILES['files']. Each element in this array corresponds to a single uploaded file and retains the standard metadata structure: name, type, tmp_name, size, and error.

If you omit the brackets (e.g., name="files"), PHP still accepts multiple files, but it collapses them into a single string separated by null bytes (\0). You would then have to use explode() to separate the filenames, which is inefficient and prone to errors if file names contain special characters or are empty. The array syntax is the robust standard for handling batch uploads.

On the server side, iterating over this array requires a specific approach. A common mistake is trying to access $_FILES['files'][0] directly without checking the count, which assumes at least one file was selected. Always verify that files were actually uploaded before processing them.

$uploadedFiles = $_FILES['files'];

// Check if any files were uploaded and no errors occurred
if ($uploadedFiles['error'] === UPLOAD_ERR_OK && $uploadedFiles['name']) {
    foreach ($uploadedFiles['name'] as $file) {
        // Process each file individually
        move_uploaded_file($file, "uploads/" . $file);
    }
}

The $_FILES superglobal is a multidimensional array. When multiple files are uploaded, the structure nests the metadata for each file inside the main key. Specifically, $_FILES['files']['name'], $_FILES['files']['type'], $_FILES['files']['tmp_name'], $_FILES['files']['size'], and $_FILES['files']['error'] will all be arrays containing the data for each respective file in the order they were selected.

One significant pitfall to address is memory consumption. Uploading multiple large files simultaneously can exhaust the server's available memory before the script even finishes writing the files to disk. PHP has strict limits defined in php.ini, such as upload_max_filesize and post_max_size. If a single file exceeds upload_max_filesize, it will be rejected. However, if the cumulative size of all uploaded files exceeds post_max_size, the entire request will fail with an error code of UPLOAD_ERR_INEXIST.

Additionally, checking $_FILES['files']['error'] inside your loop is insufficient because that key returns the error status for the first file only. You must iterate through the array keys provided by the metadata arrays themselves. Since count($_FILES['files']['name']) gives you the total number of files, looping through that count ensures you process every item correctly.

For developers using the Laravel framework, handling multiple uploads is streamlined but requires specific configuration. The request()->file('files') helper returns an instance of the UploadedFileCollection. This collection allows you to iterate over each file easily and move them individually without manually managing the raw $_FILES array structure.

use Illuminate\Support\Facades\Storage;

$request->validate([
    'files' => 'required|array|max:10',
    'files.*' => 'file|mimes:jpg,png,jpeg|max:2048'
]);

foreach ($request->file('files') as $file) {
    Storage::disk('public')->putFileAs(
        'uploads',
        $file,
        $file->getClientOriginalName()
    );
}

In this Laravel example, the validation rule required|array ensures that the field contains multiple files. The max:10 limits the number of files accepted per request to prevent denial-of-service attacks via massive file uploads. This approach abstracts away the raw $_FILES handling while maintaining security and performance controls.

Our network