Analysis on the problem of Thinkphp3.2 simply solving the problem of uploading multiple files and uploading only one file

不言
Release: 2023-03-30 18:28:02
Original
1854 people have browsed it

The following article brings you an article using Thinkphp3.2 to simply solve the problem of uploading only one file when uploading multiple files. The content is quite good, so I will share it with you now and give it as a reference.

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

Upload result display:

When many people upload multiple files, they finally find that only one file was uploaded. This is mainly due to the naming, because it is the same name, so there is only one picture left in the end

Solution:
First method:

$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

Empty saveRule in $config, after uploading The name 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 $config: 'saveName' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())),

The last The 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 and convenient method!

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

thinkphp5 method of uploading images and generating thumbnails

ThinkPHP3.2.3 verification code display and Refresh and verify

THinkPHP method of obtaining client IP and IP address query

The above is the detailed content of Analysis on the problem of Thinkphp3.2 simply solving the problem of uploading multiple files and uploading only one file. 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