First of all, let me introduce to you the multiple attribute of file in html5
Definition and Usage
The multiple attribute specifies that the input field can select multiple values. If this attribute is used, the field can accept multiple values.
Example:
<form action="demo_form.asp" method="get"> Select images: <input type="file" name="img" multiple="multiple" /> <input type="submit" /> </form>
The input file in the above example can accept multiple file upload fields.
Understanding the multiple attribute of file in html5, let’s start to explain how to use html5 to upload multiple files.
Example code:
html:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <form action="my_parser.php" method="post" enctype="multipart/form-data"> <p><input name="upload[]" type="file" multiple="multiple" /></p> <input type="submit" value="Upload all files"> </form> </body> </html>
php code:
for($i=0; $i<count($_FILES['upload']['name']); $i++) { //Get the temp file path $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; //Make sure we have a filepath if ($tmpFilePath != ""){ //Setup our new file path $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i]; //Upload the file into the temp dir if(move_uploaded_file($tmpFilePath, $newFilePath)) { //Handle other code here } } }
Thanks for reading, I hope it can help everyone, thank you for your support of this site!