php multiple file upload

Introduces the process of uploading a single file in PHP. But sometimes, for convenience, we need to meet the need to upload multiple files at the same time. The principle of multi-file upload is the same, but when processing data, the uploaded data needs to be specially processed.

<html> 
   <head> 
       <meta charset="utf-8" /> 
       <title>单文件上传</title> 
   </head> 
   <body> 
       <form action="morefile.php" method="post" enctype="multipart/form-data"> 
        <input type="file" name="file[]"> 
        <input type="file" name="file[]"> 
        <input type="submit" value="上传"> 
    </form> 
   </body> 
</html>

Here is a simple upload page, and the form submits two files at the same time. We can submit content through this page.

Note:

1.Input type="file" name="file[]"Compared with before, an extra square bracket is added after file

2.Write 2 or more input type="file" name="file[]"
We use $_FILES to receive file information, print and view the array:

<?php 
var_dump($_FILES); //打印$_FILES查看数组结构 
?>

The array structure is as follows

array (size=1)  
    'file' =>  
        array (size=5) 
    'name' =>  
        array (size=2) 
        //文件名 
        0 => string 'psu.jpg' (length=7) 
        1 => string 'qwe.jpg' (length=7) 
    //文件mime类型 
    'type' => array (size=2) 
            0 => string 'image/jpeg' (length=10) 
            1 => string 'image/jpeg' (length=10) 
    //缓存文件 
    'tmp_name' =>  
        array (size=2) 
            0 => string 'E:\wamp\tmp\phpF6D5.tmp' (length=23) 
            1 => string 'E:\wamp\tmp\phpF6F5.tmp' (length=23) 
    //文件错误信息 
    'error' =>  
        array (size=2) 
            0 => int 0 
            1 => int 0 
    //文件大小 
    'size' =>  
        array (size=2) 
        0 => int 225824     
        1 => int 151651

We can see that the two files are stored in an array, and the key names are the same as the uploaded single file. Therefore, we need to use a for() loop to retrieve the required data from the two files respectively.

The data of two files are saved in $_FILES at the same time. We need to use a simple loop to read the information of a single file and move the file to the location we want to put.

<?php
for ($i=0; $i < count($_FILE['file']['name']); $i++) {  

/* 
用is_uploaded_file()函数判断是上传文件 
并且没有出现错 
*/ 

   if(is_uploaded_file($_FILEs['file']['tmp_name'][$i]) && $_FILEs['file']['error'][$i] == 0){     
       if(move_uploaded_file($_FILEs['file']['tmp_name'][$i],'upload/'.$_FILE['file']['name'][$i])){
   //用move_uploaded_file()函数移动文件到指定的位置并使用文件原名 
   echo "上传成功"; 

       }else{ 

           echo '上传失败'; 

       } 

   }else{ 

       echo '上传失败'; 

   } 

} 
?>

For the detailed judgment process, see single file upload. Only basic judgment is made here, and there is no reminder about the file size and format.
Please judge the file size and format by yourself according to the business and provide error reminders.

Continuing Learning
||
<?php var_dump($_FILES); //打印$_FILES查看数组结构 ?>
submitReset Code