File upload: File upload: File upload: File upload:

Home >Backend Development >PHP Tutorial >Thinkphp3.2 solves the problem of uploading only one file when uploading multiple files

Thinkphp3.2 solves the problem of uploading only one file when uploading multiple files

一个新手
一个新手Original
2017-10-03 05:58:141543browse

html Simple page:

index.html Code:


##

<form action="{:U(&#39;index/upload&#39;)}" method="post" enctype="multipart/form-data">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    文件上传:<input type="file" name = "test[]">
    <input type="submit" value = "提交"></form>

Controller IndexController.class.php Code:


<?php
namespace Home\Controller;use Think\Controller;class IndexController extends Controller {    
    public function index(){        
    $this->display();
    }    
    public function upload(){        
        if(IS_POST){            
        $config = array(                
              &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;savePath&#39;   =>    &#39;&#39;,
                &#39;saveName&#39;   =>    array(&#39;uniqid&#39;, mt_rand(1,999999).&#39;_&#39;.md5(uniqid())),
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
            );            
            $upload = new \Think\Upload($config);// 实例化上传类
                  $info   =   $upload->upload();            
                  if(!$info) {                
                  $this->error($upload->getError());
            }else{                
                foreach($info as $file){                    
                echo $file[&#39;savepath&#39;].$file[&#39;savename&#39;];
                }
            }
        }else{            
            $this->display();
        }
    }
}

The upload result shows:

When many people are uploading multiple files, In the end, I found that I only uploaded one picture, mainly because of the naming. Because it was the same name, there was only one picture left in the end.

Solution: The first one:

##

$config = array(                
                &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
                &#39;saveRule&#39;   => &#39;&#39;,
            );

Leave the saveRule in $config blank, and the name after uploading is: 59c8d38cdb968.jpg

If you feel that this naming is unreliable, you can adopt the second method:

$config = array(                
                &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;saveName&#39;   =>    array(&#39;uniqid&#39;, mt_rand(1,999999).&#39;_&#39;.md5(uniqid())),
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
            );

Set in $config: 'saveName' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid()) ),

The final result is similar to: 672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg


##Of course, the naming can be modified as needed. There are many ways to upload multiple files, here is just a simple one Convenient method!

The above is the detailed content of Thinkphp3.2 solves the problem of uploading only one file when uploading multiple files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn