• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Thinkphp3.2中多文件上传只上传一张的问题解决

php 搞代码 3年前 (2022-01-22) 27次浏览 已收录 0个评论

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'    =>  <mark style="color:transparent">来4源gaodaimacom搞#代%码*网</mark><code>搞代gaodaima码</code>  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

然,命名可根据需要自行修改,多文件上传方法很多,这里只是提供个简单便捷的方法!

以上就是Thinkphp3.2中多文件上传只上传一张的问题解决的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Thinkphp3.2中多文件上传只上传一张的问题解决

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址