Home >Backend Development >PHP Tutorial >How to Upload Multiple Files Using HTML and PHP via HTTP POST?
Uploading Multiple Files with HTML and PHP via HTTP POST
Problem:
Users want to select and upload multiple files at once using a single input control, utilizing HTML and PHP via HTTP POST.
Solution:
HTML5 introduces the multiple attribute for the element, enabling the selection of multiple files. Here's an example:
<input type="file" name="my_file[]" multiple>
In PHP, the uploaded files information is accessible in the $_FILES array. The following code demonstrates how to loop through and display the details:
<?php if (isset($_FILES['my_file'])) { $myFile = $_FILES['my_file']; $fileCount = count($myFile["name"]); for ($i = 0; $i < $fileCount; $i++) { echo "<p>File #" . ($i + 1) . ":</p>"; echo "<p>"; echo "Name: " . $myFile["name"][$i] . "<br>"; echo "Temporary file: " . $myFile["tmp_name"][$i] . "<br>"; echo "Type: " . $myFile["type"][$i] . "<br>"; echo "Size: " . $myFile["size"][$i] . "<br>"; echo "Error: " . $myFile["error"][$i] . "<br>"; echo "</p>"; } } ?>
Example:
Test
Upon selecting multiple files and submitting the form, the PHP code will process each file individually, displaying its details.
Note: Proper handling of file uploads in PHP, including security considerations and error checking, is essential and should be implemented in real-world applications. Refer to the PHP Manual for more information.
The above is the detailed content of How to Upload Multiple Files Using HTML and PHP via HTTP POST?. For more information, please follow other related articles on the PHP Chinese website!