HTML Simple page:
Index.html Code:
<form action= "{: U (' Index/upload ')}" method= "Post" enctype= "Multipart/form-data" > file Upload: <input type= " File "name =" test[] "> upload: <input type=" file "name =" test[] "> file Upload: <input type=" file "name =" test[ ] > File upload: <input type= "file" name = "test[]" > file Upload: <input type= "file" name = "test[]" > file Upload: <input type= "file" name = "test[" > file Upload: <input type= "file" name = "test[" "> File Upload: <input type=" File "name =" test[] "> upload: <input type=" file "name =" test[] "> file Upload: <input type=" file "name =" test[ ] > File upload: <input type= "file" name = "test[]" > file Upload: <input type= "file" name = "test[]" > <input type= "Submit" value = "Commit" ></form>
Controller IndexController.class.php Code:
<?phpnamespace home\controller;use Think\controller;class Indexcontroller extends Controller {public function I Ndex () {$this->display (); The Public Function upload () {if (is_post) {$config = array ( ' MaxSize ' + 3145728, ' rootpath ' + './uploads/', ' savepath ' = ', ' savename ' = = Array (' Uniqid ', Mt_rand (1,999999). ' _ '. MD5 (Uniqid ())), ' exts ' = = Array (' jpg ', ' gif ', ' PNG ', ' jpeg '), ' autosub ' =& Gt True, ' subname ' = = Array (' Date ', ' YMD '),); $upload = new \think\upload ($config);//instantiation of upload class $info = $upload->upload (); if (! $info) {$this->error ($upload->geterror ()); }else{foreach ($info as $file) {echo $file [' Savepath ']. $file [' Savename ']; }}}else{$this->display (); } }}
Upload Results display:
Many people in the multi-file upload, the last to find just upload a, mainly because of the name, because it is the same names, so the last picture left
Workaround: The first type:
$config = Array ( ' maxSize ' = = 3145728, ' rootpath ' = ' ./uploads/', ' exts ' = = Array (' jpg ', ' gif ', ' PNG ', ' jpeg '), ' autosub ' = ' = True ', ' subname ' Array (' Date ', ' YMD '), ' saverule ' = ', ' ;
Empty $config inside the Saverule, after the name of the upload: 59c8d38cdb968.jpg
If you feel this name is unreliable, you can take a second approach:
$config = Array ( ' maxSize ' = = 3145728, ' rootpath ' = ' ./uploads/', ' savename ' = = Array (' Uniqid ', Mt_rand (1,999999). ' _ '. MD5 (Uniqid ())), ' exts ' = = Array (' jpg ', ' gif ', ' PNG ', ' jpeg '), ' autosub ' = True, ' subname ' = = Array (' Date ', ' YMD '), );
Set $config: ' savename ' = = Array (' Uniqid ', Mt_rand (1,999999). ' _ '. MD5 (Uniqid ())),
The final results are similar to: 672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg
However, the name can be modified as needed, many file upload method, here is just a simple and convenient way!