The process for uploading multiple files is the same as that for uploading individual files. You only need to provide multiple input forms of the "file" type on the client and specify different "name" attribute values. For example, in the following code, you can upload three local files to the server at the same time. The form of the client is as follows:
The code is as follows: |
Copy code |
<Html> <Head> <title> multiple file upload forms </title> <Body> <Form action = "mul_upload.php" method = "post" enctype = "multipart/form-data"> <Input type = "hidden" name = "MAX_FILE_SIZE" value = "1000000"> Select file 1: <input type = "file" name = 'myfile [] '> <br> Select file 2: <input type = "file" name = 'myfile [] '> <br> Select file 3: <input type = "file" name = 'myfile [] '> <br> <Input type = "submit" value = "Upload file"> </Form> </Body> </Html> |
In the above code, three file types of forms are organized in the form of arrays. When the above form teaches PHP the script file mul_upload.php, the server also uses a global array $ _ FILES to store all the information of the above FILES, however, $ _ FILES has been changed from a two-dimensional array to a three-dimensional array, so that the information of multiple uploaded FILES can be stored. In the script file mul_upload.php, use the print_r () function to output the content in the $ _ FILES array. The code is as follows:
The code is as follows: |
Copy code |
<? Php // Print the content in the 3D array $ _ FILES and check the structure of the uploaded file. Print_r ($ _ FILES ); ?> |
After three local files are selected for submission, the output result is as follows:
The code is as follows: |
Copy code |
Array ( [Myfile] => Array ( [Name] => Array (--- $ _ FILES ["myfile"] ["name"] stores the content of all uploaded FILES [0] => Rav. ini --- $ _ FILES ["myfile"] ["name"] [0] name of the first uploaded File [1] => msgsocm. log --- $ _ FILES ["myfile"] ["name"] [1] name of the second uploaded File [2] => NOTEPAD. EXE) --- $ _ FILES ["myfile"] ["name"] [2] name of the third uploaded File [Type] => Array (--- $ _ FILES ["myfile"] ["type"] stores all types of uploaded FILES [0] => application/octet-stream --- $ _ FILES ["myfile"] ["type"] [0] type of the first file to be uploaded [1] => application/octet-stream --- $ _ FILES ["myfile"] ["type"] [1] type of the second uploaded File [2] => application/octet-stream) --- $ _ FILES ["myfile"] ["type"] [2] type of the third object to be 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] = & gt; 66560 )) ) |
The output value of the $ _ FILES array shows that the process of uploading multiple FILES is the same as that of uploading a single file, but the structure of the $ _ FILES array is slightly different. In this way, more files can be uploaded.
Example
The code is as follows: |
Copy code |
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <Title> document Upload </title> </Head> <Body> <Script language = "javascript"> <! -- Dynamically add a file selection control --> Function AddRow () { Var eNewRow = tblData. insertRow (); For (var I = 0; I <1; I ++) { Var eNewCell = eNewRow. insertCell (); ENewCell. innerHTML = "<tr> <td> <input type = 'file' name = 'filelist [] 'size = '50'/> </td> </tr> "; } } // --> </Script> <Form name = "myform" method = "post" action = "uploadfile. php" enctype = "multipart/form-data"> <Table id = "tblData" width = "400" border = "0"> <! -- The post method and enctype = "multipart/form-data" must be used to upload files. --> <! -- Pass the URL on this page to uploadfile. php --> <Input name = "postadd" type = "hidden" value = "<? Php echo "http: //". $ _ SERVER ['http _ host']. $ _ SERVER ["PHP_SELF"];?> "/> <Tr> <td> File upload list <Input type = "button" name = "addfile" onclick = "AddRow ()" value = "Add List"/> </td> </tr> <! -- Filelist [] must be an array --> <Tr> <td> <input type = "file" name = "filelist []" size = "50"/> </td> </tr> </Table> <Input type = "submit" name = "submitfile" value = "submit File"/> </Form> </Body> </Html> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <Title> document Upload </title> </Head> <Body> <Script language = "javascript"> <! -- Dynamically add a file selection control --> Function AddRow () { Var eNewRow = tblData. insertRow (); For (var I = 0; I <1; I ++) { Var eNewCell = eNewRow. insertCell (); ENewCell. innerHTML = "<tr> <td> <input type = 'file' name = 'filelist [] 'size = '50'/> </td> </tr> "; } } // --> </Script> <Form name = "myform" method = "post" action = "uploadfile. php" enctype = "multipart/form-data"> <Table id = "tblData" width = "400" border = "0"> <! -- The post method and enctype = "multipart/form-data" must be used to upload files. --> <! -- Pass the URL on this page to uploadfile. php --> <Input name = "postadd" type = "hidden" value = "<? Php echo "http: //". $ _ SERVER ['http _ host']. $ _ SERVER ["PHP_SELF"];?> "/> <Tr> <td> File upload list <Input type = "button" name = "addfile" onclick = "AddRow ()" value = "Add List"/> </td> </tr> <! -- Filelist [] must be an array --> <Tr> <td> <input type = "file" name = "filelist []" size = "50"/> </td> </tr> </Table> <Input type = "submit" name = "submitfile" value = "submit File"/> </Form> </Body> </Html> Submit file code View plaincopy to clipboardprint? <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/> <Title> File upload Result </title> </Head> <Body> <? Php If ($ _ POST ["submitfile"]! = "") { $ Path = "./". date ('ym ')."/"; If (! Is_dir ($ Path) // create a Path {Mkdir ($ Path );} Echo "<div> "; For ($ I = 0; $ I <count ($ filelist); $ I ++) {// $ _ FILES ["filelist"] ["size"] [$ I] cannot be changed because fileist is a two-dimensional array. If ($ _ FILES ["filelist"] ["size"] [$ I]! = 0) { $ File = $ Path. date ('ymdhm'). "_". $ _ FILES ["filelist"] ["name"] [$ I]; If (move_uploaded_file ($ _ FILES ["filelist"] ["tmp_name"] [$ I], $ File )) {Echo "file uploaded successfully:". $ _ FILES ["filelist"] ["type"] [$ I]. "". "File name :" . $ _ FILES ["filelist"] ["name"] [$ I]. "<br> ";} Else {Echo "File name:". $ _ FILES ["filelist"] ["name"] [$ I]. "Upload failed </br> ";} } } Echo "</div> <br> <a href =" $ postadd "href =" $ postadd "> Return </a> </div> "; } ?> </Body> </Html> |
In the above example, the file upload box is dynamically added based on js to achieve the multi-file upload function.