Home  >  Article  >  Backend Development  >  php uses html5 to implement multiple file uploads

php uses html5 to implement multiple file uploads

不言
不言Original
2018-06-05 16:05:553932browse

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

  }
 }
}

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

Related recommendations:

Detailed examples of how to convert html word to and from PHP

The above is the detailed content of php uses html5 to implement multiple file uploads. 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