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

黄舟
发布: 2017-09-26 09:08:19
原创
1651人浏览过

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代码:


<?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();
        }
    }
}
登录后复制

上传结果显示:

立即学习PHP免费学习笔记(深入)”;

好多人在进行多文件上传的时候,最后发现只是上传了一张,主要就是命名所致,因为是同样的名字,所以最后就剩一张图片
解决方法:第一种:


$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

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

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

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号