Home  >  Article  >  Backend Development  >  php uses html5 to implement multiple file upload examples_php examples

php uses html5 to implement multiple file upload examples_php examples

WBOY
WBOYOriginal
2016-12-05 13:28:161180browse

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!

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