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: <inpu T name= "upload_file[" "type=" File "size="/></td> </tr> <tr> <td> upload File: <input name= "upload_file[" "type=" File "size=" "/></td> </tr> <tr> &            lt;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> & Lt;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:


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.

Related recommendations:

PHP File upload function for code sharing

Configure PHP.ini to implement PHP file upload function

How to configure PHP.ini to implement PHP file upload function

Related Article

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: 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.