File upload: File upload: File upload: File upload:
Home >Backend Development >PHP Tutorial >Thinkphp3.2 solves the problem of uploading only one file when uploading multiple files
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(); } } }The upload result shows: When many people are uploading multiple files, In the end, I found that I only uploaded one picture, mainly because of the naming. Because it was the same name, there was only one picture left in the end.
Solution: The first one:
##
$config = array( 'maxSize' => 3145728, 'rootPath' => './Uploads/', 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autoSub' => true, 'subName' => array('date','Ymd'), 'saveRule' => '', );
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'), );
##Of course, the naming can be modified as needed. There are many ways to upload multiple files, here is just a simple one Convenient method!
The above is the detailed content of Thinkphp3.2 solves the problem of uploading only one file when uploading multiple files. For more information, please follow other related articles on the PHP Chinese website!