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

一个新手
Release: 2023-03-16 13:48:01
Original
1541 people have browsed it

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>
Copy after login

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();
        }
    }
}
Copy after login

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;,
            );
Copy after login

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;),
            );
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template