Thinkphp3.2 solve the problem of uploading a single file with multiple files upload method

Source: Internet
Author: User
HTML Simple page:

Index.html Code:


<form action= "{: U (' Index/upload ')}" method= "Post" enctype= "Multipart/form-data" >    file Upload: <input type= " File "name =" test[] ">    upload: <input type=" file "name =" test[] ">    file Upload: <input type=" file "name =" test[ ] >    File upload: <input type= "file" name = "test[]" >    file Upload: <input type= "file" name = "test[]" >    file Upload: <input type= "file" name = "test[" >    file Upload: <input type= "file" name = "test[" ">    File Upload: <input type=" File "name =" test[] ">    upload: <input type=" file "name =" test[] ">    file Upload: <input type=" file "name =" test[ ] >    File upload: <input type= "file" name = "test[]" >    file Upload: <input type= "file" name = "test[]" >    <input type= "Submit" value = "Commit" ></form>

Controller IndexController.class.php Code:


<?phpnamespace home\controller;use Think\controller;class Indexcontroller extends Controller {public function I    Ndex () {$this->display ();                              The Public Function upload () {if (is_post) {$config = array (    ' MaxSize ' + 3145728, ' rootpath ' + './uploads/', ' savepath ' = ', ' savename ' = = Array (' Uniqid ', Mt_rand (1,999999). ' _ '. MD5 (Uniqid ())), ' exts ' = = Array (' jpg ', ' gif ', ' PNG ', ' jpeg '), ' autosub ' =&    Gt                        True, ' subname ' = = Array (' Date ', ' YMD '),);                              $upload = new \think\upload ($config);//instantiation of upload class $info = $upload->upload ();            if (! $info) {$this->error ($upload->geterror ()); }else{foreach ($info as $file) {echo $file [' Savepath ']. $file [' Savename '];        }}}else{$this->display (); }    }}

Upload Results display:

Many people in the multi-file upload, the last to find just upload a, mainly because of the name, because it is the same names, so the last picture left
Workaround: The first type:


$config = Array (                                ' maxSize ' = =    3145728,                ' rootpath ' = '    ./uploads/',                ' exts '       = =    Array (' jpg ', ' gif ', ' PNG ', ' jpeg '),                ' autosub '    = ' = True ',                ' subname '    Array (' Date ', ' YMD '),                ' saverule '   = ', '            ;

Empty $config inside the Saverule, after the name of the upload: 59c8d38cdb968.jpg

If you feel this name is unreliable, you can take a second approach:


$config = Array (                                ' maxSize ' = =    3145728,                ' rootpath ' = '    ./uploads/',                ' savename '   = =    Array (' Uniqid ', Mt_rand (1,999999). ' _ '. MD5 (Uniqid ())),                ' exts '       = =    Array (' jpg ', ' gif ', ' PNG ', ' jpeg '),                ' autosub '    =    True,                ' subname '    = =    Array (' Date ', ' YMD '),            );

Set $config: ' savename ' = = Array (' Uniqid ', Mt_rand (1,999999). ' _ '. MD5 (Uniqid ())),
The final results are similar to: 672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg

However, the name can be modified as needed, many file upload method, here is just a simple and convenient way!

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.