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.
After understanding the multiple attribute of file in html5, let's start to explain how to use html5 to implement multiple file uploads.
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!
For more php related articles using html5 to implement multiple file upload examples, please pay attention to the PHP Chinese website!