Why Single Input Multiple Uploads Fail in PHP
Developers often assume that because an HTML <input type="file"> element is present, the server can accept multiple files automatically. In reality, a standard file input without specific attributes submits exactly one file per request. To handle multiple uploads through a single field, you must explicitly enable the multiple attribute in the HTML and correctly parse the resulting array on the backend.
The HTML Requirement
The most common oversight is forgetting to add multiple="multiple" (or simply multiple) to the input tag. Without this attribute, the browser sends only the first file selected via drag-and-drop or the last file chosen from a dialog, regardless of how many files were highlighted in the UI before submission.
<!-- Single file upload -->
<input type="file" name="upload">
<!-- Multiple file upload required -->
<input type="file" name="upload" multiple>
When this attribute is present, the browser packages all selected files into a single multipart/form-data request. The $_FILES superglobal reflects this by containing arrays instead of scalar values for keys like name, size, tmp_name, and error.
Parsing the $_FILES SuperGlobal
Your PHP script attempts to iterate over $_FILES["upload"]["tmp_name"], which is the correct approach. However, there are critical flaws in the logic surrounding file validation and error handling that prevent reliable execution.
The Name Check Logic
The condition if (($_FILES["upload"]["name"] < 90000000000000000)) compares a string containing filenames to a numeric integer. In PHP, this triggers type juggling where the filename is cast to an integer. If the filename starts with non-numeric characters (which is typical), the result is 0. This means the condition effectively evaluates based on whether the resulting integer is less than 900 quadrillion, which is almost always true for filenames but semantically meaningless.
For actual validation, you should rely on the error index or check the file size via $_FILES["upload"]["size"]. Always validate the error code first; a value of 0 indicates success, while UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, or other constants indicate specific failure modes.
The Extension Check
The logic in_array($extension, $allowedExts) relies on explode(".", $_FILES["upload"]["name"]). This is fragile because it only checks the final dot in the filename. A file named image.jpg.backup would incorrectly pass as a valid image extension if your check isn't more robust. While not the primary blocker for multiple uploads, this should be refactored to ensure only the actual extension is evaluated.
The Loop and Error Handling Trap
Your script includes a loop:
for($i=0; $i < count($_FILES['upload']['tmp_name']);$i++) { ... }
Theoretically, this iterates over every uploaded file. However, the logic inside the if block checks $_FILES["upload"]["error"] without indexing it with $i.
If the first file is valid (error 0), but the second file has an error or fails size validation, your code still proceeds to the else block where move_uploaded_file executes. More critically, if any iteration encounters a logic error or exception that isn't caught, the entire upload process halts. Because the script checks $_FILES["upload"]["name"] inside the loop without the index (e.g., using $i[0] implicitly via array access), it might be re-evaluating the same scalar values or accessing incorrect indices depending on how PHP handles the superglobal structure in that specific context.
To ensure every file is processed independently, you must explicitly access the array index for every property inside the loop:
$name = $_FILES['upload']['name'][$i];
$size = $_FILES['upload']['size'][$i];
$error = $_FILES['upload']['error'][$i];
$tmp_name = $_FILES['upload']['tmp_name'][$i];
Security and Naming Conflicts
Your script generates a random filename:
$file = rand(1,999999999999) . '.' .end($temp);
Using rand() is insecure for file uploads because an attacker can predict the sequence if they know the algorithm or seed, potentially overwriting existing files. Furthermore, generating random names makes it impossible to retrieve the original filename later if needed for display.
A safer approach involves validating the extension strictly against a whitelist and renaming the file using a cryptographic hash (like hash_file('sha256', $tmp_name)) combined with the allowed extension. This prevents directory traversal attacks and ensures unique filenames without relying on weak random number generators.
Conclusion
The reason your single-input multiple upload fails is likely a combination of missing HTML attributes and incorrect array indexing within the PHP loop. By enabling multiple in the form, validating each file's error code individually inside the iteration, and securing the filename generation process, you can build a robust multi-file uploader. For more scalable solutions involving complex file management or storage abstraction, consider leveraging frameworks like Laravel, which offer built-in traits for handling these workflows efficiently.
