Home > Backend Development > PHP Tutorial > PHP solution to the problem of the same name when uploading images or files in batches in TP3.2

PHP solution to the problem of the same name when uploading images or files in batches in TP3.2

巴扎黑
Release: 2023-03-14 17:24:02
Original
2086 people have browsed it

The example in this article shares with you how to solve the problem of name conflict when uploading files or pictures in batches in TP3.2 for your reference. The specific content is as follows

1, html

<form action="{:U(&#39;Upload/index&#39;)}" enctype="multipart/form-data" method="post" >
  <p><input type="file" id="file3" name="ID[]" /></p>
  <p><input type="file" id="file4" name="ID[]" /></p>
  <input type="submit" value="上传" />
  <p><img id="img1" alt="" src="/Public/IMAGE/empty_thumb.gif" /></p>
 </form>
Copy after login

2, php

public function index(){
       if(!empty($_FILES)){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;
        $upload->rootPath = &#39;./Uploads/&#39;;
        $upload->savePath = &#39;image/&#39;;
        //$upload->saveName = date(&#39;YmdHis&#39;).&#39;-&#39;.randomkeys(3);//msectime(),毫秒数13位
        $upload->saveName = &#39;msectime&#39;;   //自定义函数,采用13位毫秒和3位随机数
        $upload->exts   = array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;);
        $upload->autoSub = true;
        $upload->subName = array(&#39;date&#39;,&#39;Ymd&#39;);
        
        /* 判断$_FILES[$key]是否:一维数组,单张图片上传 -xzz0703 
         * 原理:html的input type = "file" name="IDcard"和name="IDcard[]"的区别:
         *    $_FILES前者到后台php是二维数组,后者是三维数组 
        */
        foreach($_FILES as $key=>$value){
          if(count($_FILES[$key]) == count($_FILES[$key],1)){
            $info = $upload->uploadOne($_FILES[$key]);
            if($info){
              echo json_encode(array(&#39;code&#39;=>200,&#39;id&#39;=>$img_id,&#39;name&#39;=>$img_name));exit;
            }else{
              echo json_encode(array(&#39;code&#39;=>0,&#39;msg&#39;=>$upload->getError()));exit;
            }
          }
        }
        if(count($_FILES)){
          $info = $upload->upload();//如果是二维数组,使用批量上传文件的方法
          if(!$info){
            $this->error($upload->getError());
            exit;
          }
          $img_url = &#39;/Uploads/&#39;.$info[0][&#39;savepath&#39;].$info[0][&#39;savename&#39;];
          $res = array(&#39;imgPath1&#39;=>$img_url,code=>$img_url,&#39;msg&#39;=>$info);
          echo json_encode($res);
        }        
      }   
}
Copy after login

3. Core: Many friends are stuck on the saveName attribute when using the TP3.2 framework. The reason is that the upload server processing level takes millions of microseconds. soon.

Solution: saveName = 13 digits of milliseconds + 3 digits of random number, perfect solution, specific code:

//返回当前的毫秒时间戳和随机数合并的字符串
function msectime() {
  list($msec, $sec) = explode(&#39; &#39;, microtime());
  $msectime = (float)sprintf(&#39;%.0f&#39;, (floatval($msec) + floatval($sec)) * 1000).randomkeys(3);
  return $msectime;
}
Copy after login

The above is the detailed content of PHP solution to the problem of the same name when uploading images or files in batches in TP3.2. 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