About Thinkphp3.2 Simple resolution of multi-file upload and upload only one of the problem resolution

Source: Internet
Author: User
The following for everyone to bring a Thinkphp3.2 simple solution to upload a multi-file upload only one problem. Content is very good, now share to everyone, but also for everyone to make a reference.

HTML Simple page:

Index.html Code:

<form action= "{: U (' Index/upload ')}" method= "Post" enctype= "multipart/form-data" > File upload: <input type= "Files" 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 = "T est[] "> Upload: <input type=" file "name =" test[] "> File upload: <input type=" file "name =" test[] "> File upload: <input ty pe= "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 = "Submit" ></form>

Controller IndexController.class.php Code:

<?phpnamespace home\controller;use Think\controller;class Indexcontroller extends Controller {public Function index () {  $this->display ();} 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 ' = "= 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!

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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