This article is mainly for you to introduce the TP3.2 batch upload files or pictures, the same name conflict resolution method, with a certain reference value, interested in small partners can refer to
This example for everyone to share the TP3.2 batch upload files or pictures of the specific code, and resolved 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;}