Home > Backend Development > PHP Tutorial > Implementation example of php multiple file upload

Implementation example of php multiple file upload

高洛峰
Release: 2023-03-04 19:44:01
Original
1403 people have browsed it

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.

Let’s look at an example of multiple file upload:

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

php file my_parser.php

<?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>";
    }
  }
}
?>
Copy after login

Thanks for reading, I hope this helps everyone, and thank you for your support of this site!

For more articles related to PHP multi-file upload implementation examples, please pay attention to the PHP Chinese website!

Related labels:
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