PHP resolves the problem with the same name when uploading images or files in bulk TP3.2

Source: Internet
Author: User
The example of this article for you to share how to solve the TP3.2 batch upload files or images with the same name conflict, for your reference, the specific content as follows

1. html

<form action= "{: U (' Upload/index ')}" 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= "Upload"/>  <p></p> </form>

2. php

Public Function Index () {if (!empty ($_files)) {$upload = new \think\upload ();//Instantiate upload class $upload->maxs        ize = 3145728;        $upload->rootpath = './uploads/';        $upload->savepath = ' image/'; $upload->savename = Date (' Ymdhis '). '   -'. Randomkeys (3);//msectime (), 13 bits of milliseconds $upload->savename = ' msectime ';        Custom functions, using 13-bit milliseconds and 3-bit random numbers $upload->exts = array (' jpg ', ' gif ', ' PNG ', ' jpeg ');        $upload->autosub = true;                $upload->subname = Array (' Date ', ' Ymd ');         /* Determine $_files[$key] whether: one-dimensional array, single picture upload-xzz0703 * principle: HTML input type = "File" name= "Idcard" and Name= "idcard[" The Difference:  * $_files the former to the background PHP is a two-dimensional array, which is a three-dimensional array */foreach ($_files as $key + $value) {if (count ($_files[$key])            = = Count ($_files[$key],1)) {$info = $upload->uploadone ($_files[$key]);            if ($info) {echo json_encode (' Code ' =>200, ' id ' = = $img _id, ' name ' = = $img _name); exit; }else{echo json_encode (Array (' Code ' =>0, ' msg ' = + $upload->geterror ()); exit; }}} if (count ($_files)) {$info = $upload->upload ();//If it is a two-dimensional array, use the bulk upload file method if (!            $info) {$this->error ($upload->geterror ());          Exit          } $img _url = '/uploads/'. $info [0][' Savepath ']. $info [0][' Savename '];          $res = Array (' imgPath1 ' = = $img _url,code=> $img _url, ' msg ' = $info);        echo Json_encode ($res); }              }   }

3, Core: Many friends in the use of the TP3.2 framework, the Savename attribute is stuck, the reason is to upload the server processing level of millions of microseconds, soon.

Resolution:Savename = 13-bit millisecond + 3-bit random number, perfect solution, specific code:

Returns the current millisecond timestamp and the random number of the merged string function Msectime () {  list ($msec, $sec) = Explode ("', Microtime ());  $msectime = (float) sprintf ('%.0f ', (Floatval ($msec) + floatval ($sec)) * +). Randomkeys (3);  return $msectime;}
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.