Home  >  Article  >  Backend Development  >  PHP multi-file upload implementation example_php example

PHP multi-file upload implementation example_php example

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

First, let me explain to you how to implement it.

To implement multiple file uploads, we can add multiple input file fields to the form, and then set the name attributes of these input files to the same name and name them in the form of an array, such as filename[]. As for the php code for file upload, it is the same as single file upload.

Look at an example of uploading multiple files below:

html file example.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="my_parser.php" method="post" enctype="multipart/form-data">
 <p><input type="file" name="file_array[]"></p>
 <p><input type="file" name="file_array[]"></p>
 <p><input type="file" name="file_array[]"></p>
 <input type="submit" value="Upload all files">
</form>
</body>
</html>

php file my_parser.php

<&#63;php
if(isset($_FILES['file_array'])){
  $name_array = $_FILES['file_array']['name'];
  $tmp_name_array = $_FILES['file_array']['tmp_name'];
  $type_array = $_FILES['file_array']['type'];
  $size_array = $_FILES['file_array']['size'];
  $error_array = $_FILES['file_array']['error'];
  for($i = 0; $i < count($tmp_name_array); $i++){
    if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])){
      echo $name_array[$i]." upload is complete<br>";
    } else {
      echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
    }
  }
}
&#63;>

Thanks for reading, I hope it can help you, 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