Home  >  Article  >  Backend Development  >  How to use html5 to upload multiple files in php

How to use html5 to upload multiple files in php

墨辰丷
墨辰丷Original
2018-05-31 11:31:571643browse

Before HTML came out, it was troublesome to upload multiple files in PHP. You needed to add multiple input file fields to the form. After the release of html5, we can use the html5 attribute multiple of the input file to upload multiple files. Friends who need it can refer to it

First of all, I will introduce to you the multiple attribute of the file in html5

Definition and Usage

The multiple attribute specifies that multiple values ​​can be selected for the input field. 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[&#39;upload&#39;][&#39;name&#39;]); $i++) {
 //Get the temp file path
 $tmpFilePath = $_FILES[&#39;upload&#39;][&#39;tmp_name&#39;][$i];

 //Make sure we have a filepath
 if ($tmpFilePath != ""){
  //Setup our new file path
  $newFilePath = "./uploadFiles/" . $_FILES[&#39;upload&#39;][&#39;name&#39;][$i];

  //Upload the file into the temp dir
  if(move_uploaded_file($tmpFilePath, $newFilePath)) {

   //Handle other code here

  }
 }
}

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. At the same time, I also hope that everyone will support the PHP Chinese website.

Related recommendations:

PHP long connection use case analysis

phpUse redis long connection What are the steps

phpDetailed explanation of application containerization and deployment

The above is the detailed content of How to use html5 to upload multiple files in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn