轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
猜你喜欢
查看: 5604|回复: 1
打印 上一主题 下一主题

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

[复制链接]

0

主题

0

帖子

1万

积分

钻石会员

Rank: 8Rank: 8

积分
17424
QQ
跳转到指定楼层
楼主
发表于 2020-5-9 13:30 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
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 HomeController;use ThinkController;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 ThinkUpload($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

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

分享到:  QQ好友和群QQ好友和群
收藏收藏
回复

使用道具 举报

0

主题

22

帖子

70

积分

注册会员

Rank: 2

积分
70
沙发
发表于 2022-5-3 17:39 | 只看该作者
情侣网站源码
回复

使用道具 举报

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表