PHP File Upload Function--multi-File upload

Source: Internet
Author: User
Tags php file upload

This section mainly introduces the multi-file upload function of php upload file.

As long as you name the file upload tag in the form as an array, you can upload multiple files simultaneously.

Let's look at an example:
---------------------------------------------------------------------
<form enctype= "Multipart/form-data" action= "<?=$_server[' php_self ']?>" method= "POST" >
<input type= "hidden" name= "max_file_size" value= "104857600"/>
<table>
<tr>
<td> upload file: <input name= "upload_file[" "type=" File "size="/></td>
</tr>
<tr>
<td> upload file: <input name= "upload_file[" "type=" File "size="/></td>
</tr>
<tr>
<td> upload file: <input name= "upload_file[" "type=" File "size="/></td>
</tr>
<tr>
<td> upload file: <input name= "upload_file[" "type=" File "size="/></td>
</tr>
<tr>
<td><input type= "Submit" name= "submit" value= "Upload"/></td>
</tr>
</table>
</form>

<?php
function upload ($file _error, $file _tmp_name, $file _name) {
$info = "";

if ($file _name = = "")
return $info;

Switch ($file _error) {
Case Upload_err_ini_size:
$info = $file _name. ": The file size exceeds the server limit";
Break
Case Upload_err_form_size:
$info = $file _name. ": The file size exceeds the browser limit";
Break
Case Upload_err_partial:
$info = $file _name. ": Only part of the file was uploaded";
Break
Case Upload_err_no_file:
$info = $file _name. ": No files were uploaded";
Break
Case UPLOAD_ERR_NO_TMP_DIR:
$info = $file _name. ": Unable to find temp folder";
Break
Case Upload_err_cant_write:
$info = $file _name. ": File write Failed";
Break
Case UPLOAD_ERR_OK:
$upload _dir = './'. Iconv ("UTF-8", "gb2312", $file _name);
if (file_exists ($upload _dir)) {
$info = $file _name. ": File with the same name already exists";
}else{
if (Move_uploaded_file ($file _tmp_name, $upload _dir)) {
$info = $file _name. ": File upload succeeded";
}else{
$info = $file _name. ": File upload Failed";
}
}
Break
}
return $info;
}

if (Isset ($_post[' submit ')) {
$info = ";
$count = count ($_files[' upload_file ' [' name ']);
for ($i =0; $i < $count; + + $i) {
if ($_files[' upload_file ' [' name '] [$i] = = "")
Continue
$info = Upload (
$_files[' upload_file ' [' Error '] [$i],
$_files[' upload_file ' [' tmp_name '] [$i],
$_files[' upload_file ' [' name '] [$i]
);
}
Echo $info;
}

?>
--------------------------------------------------------------------------------------
The code execution results are as follows:


Attention:

1, <input name= "upload_file[" "type=" File "size=" "/>", name= "upload_file[" must be named in an array, or there will be an error: " Uninitialized string offset:0 ", which means that your array key value crosses the line

2, $_files[' upload_file ' [' name '] [$i], Upload_file is the name of the upload file marker in the form, and when multiple files are uploaded, the bidding clubs of $_files in the third dimension of the array is automatically numbered starting from 0.

PHP File Upload Function--multi-File upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.