Home > Article > Backend Development > Analysis on the problem of Thinkphp3.2 simply solving the problem of uploading multiple files and uploading only one file
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('index/upload')}" 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>
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( 'maxSize' => 3145728, 'rootPath' => './Uploads/', 'savePath' => '', 'saveName' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())), 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autoSub' => true, 'subName' => array('date','Ymd'), ); $upload = new \Think\Upload($config);// 实例化上传类 $info = $upload->upload(); if(!$info) { $this->error($upload->getError()); }else{ foreach($info as $file){ echo $file['savepath'].$file['savename']; } } }else{ $this->display(); } } }
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 endSolution:
First method:
$config = array( 'maxSize' => 3145728, 'rootPath' => './Uploads/', 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autoSub' => true, 'subName' => array('date','Ymd'), 'saveRule' => '', );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( '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 last The result is similar to: 672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg
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!