Form Design Example of Uploading Multiple Files in PHP

  • 2021-08-05 09:01:15
  • OfStack

Multiple file uploads and single file uploads are handled in the same way, only several input forms of type "file" need to be provided at the client, and different attribute values of "name" need to be specified. For example, in the following code, the user can select three local files at the same time and upload them to the server. The client form is as follows:


<html>
<head><title> Multiple file upload form </title></head>
<body>
<form action="mul_upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Select a file 1 : <input type="file" name='myfile[]'><br>
Select a file 2 : <input type="file" name='myfile[]'><br>
Select a file 3 : <input type="file" name='myfile[]'><br>
<input type="submit" value=" Upload a file ">
</form>
</body>
</html>

In the above code, the forms of three file types are organized in an array from 1. When the above form taught the script file mul_upload. php for PHP, the global array $_ FILES was also used on the server side to store all the above files, but $_ FILES has been converted from a 2-D array to a 3-D array, so that it can store the information of multiple uploaded files. In the script file mul_upload. php, use the print_r () function to output the contents of the $_FILES array as follows:


<?php
// Print 3 Dimensional array $_FILES View the contents in the 1 The structure of storing uploaded files under
print_r($_FILES);
?>

When you select 3 local files to submit, the output is as follows:


Array(
    [myfile]=>Array(
        [name]=>Array(              ---$_FILES["myfile"]["name"] Store the contents of all uploaded files
            [0]=>Rav.ini         ---$_FILES["myfile"]["name"][0] No. 1 1 The name of the uploaded file
            [1]=>msgsocm.log     ---$_FILES["myfile"]["name"][1] No. 1 2 The name of the uploaded file
            [2]=>NOTEPAD.EXE)        ---$_FILES["myfile"]["name"][2] No. 1 3 The name of the uploaded file
        [type]=>Array(               ---$_FILES["myfile"]["type"] Stores the types of all uploaded files
            [0]=>application/octet-stream          ---$_FILES["myfile"]["type"][0] No. 1 1 Types of files uploaded
            [1]=>application/octet-stream          ---$_FILES["myfile"]["type"][1] No. 1 2 Types of files uploaded
            [2]=>application/octet-stream)         ---$_FILES["myfile"]["type"][2] No. 1 3 Types of files uploaded
        [tmp_name]=>Array(
            [0]=>C:\WINDOWS\Temp\phpAF.tmp
            [1]=>C:\WINDOWS\Temp\phpB0.tmp
            [2]=>C:\WINDOWS\Temp\phpB1.tmp)
        [error]=>Array(
            [0]=>0
            [1]=>0
            [2]=>0)
        [size]=>Array(
            [0]=>64
            [1]=>1350
            [2]=>66560))
)

By outputting the value of the $_ FILES array, we can see that the situation when handling multiple file uploads is similar to that when uploading a single file, except that the structure of the $_ FILES array is slightly different. In this way, a larger number of file uploads can be supported.


Related articles: