Home >Backend Development >PHP Tutorial >How to Upload Multiple Files Using HTML5 and PHP?
Q: How can I select and upload multiple files using a single file input control with HTTP POST?
A: In HTML5, this is possible using the following code:
<form method="post" enctype="multipart/form-data"> <input type="file" name="my_file[]" multiple> <input type="submit" value="Upload"> </form>
PHP Code to Handle Upload:
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>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></p>"; } }
Example Output:
Suppose two files are selected:
File #1: Name: image1.jpg Temporary file: /tmp/phpXXXXXXXX Type: image/jpeg Size: 123456 Error: 0 File #2: Name: image2.jpg Temporary file: /tmp/phpXXXXXXXX Type: image/jpeg Size: 654321 Error: 0
Additional Notes:
The above is the detailed content of How to Upload Multiple Files Using HTML5 and PHP?. For more information, please follow other related articles on the PHP Chinese website!