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

php uses html5 to implement multiple file upload examples_php examples

WBOY
Release: 2023-03-03 06:00:01
Original
1192 people have browsed it

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>
Copy after login

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>
Copy after login

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

  }
 }
}

Copy after login

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template