您好,欢迎访问一九零五行业门户网

关于Thinkphp3.2简单解决多文件上传以及只上传一张的问题解析

下面为大家带来一篇thinkphp3.2简单解决多文件上传只上传一张的问题。内容挺不错的,现在就分享给大家,也给大家做个参考。
html简单页面:
index.html代码:
<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>
控制器indexcontroller.class.php代码:
<?phpnamespace 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(); } }}
上传结果显示:
好多人在进行多文件上传的时候,最后发现只是上传了一张,主要就是命名所致,因为是同样的名字,所以最后就剩一张图片
解决方法:第一种:
$config = array( 'maxsize' => 3145728, 'rootpath' => './uploads/', 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autosub' => true, 'subname' => array('date','ymd'), 'saverule' => '', );
置空$config里面的saverule,上传后的名称为:59c8d38cdb968.jpg
若是感觉这种命名不可靠,可采取第二种方法:
$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'), );
设置$config中: 'savename' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())),
其最后的结果类似于:672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg
然,命名可根据需要自行修改,多文件上传方法很多,这里只是提供个简单便捷的方法!
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
thinkphp5上传图片及生成缩略图的方法
thinkphp3.2.3验证码的显示和刷新以及校验
thinkphp获取客户端ip与ip地址查询的方法
以上就是关于thinkphp3.2简单解决多文件上传以及只上传一张的问题解析的详细内容。
其它类似信息

推荐信息