Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Thinkphp3.2中多文件上传只上传一张的问题解决

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

黄舟
黄舟asal
2017-09-26 09:08:191389semak imbas

html简单页面:

index.html代码:


<form action="{:U(&#39;index/upload&#39;)}" 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(                
    &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;savePath&#39;   =>    &#39;&#39;,
                &#39;saveName&#39;   =>    array(&#39;uniqid&#39;, mt_rand(1,999999).&#39;_&#39;.md5(uniqid())),
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
            );            $upload = new \Think\Upload($config);// 实例化上传类
            $info   =   $upload->upload();            
            if(!$info) {                
            $this->error($upload->getError());
            }else{                
            foreach($info as $file){                    
            echo $file[&#39;savepath&#39;].$file[&#39;savename&#39;];
                }
            }
        }else{            $this->display();
        }
    }
}

上传结果显示:

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


$config = array(                &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
                &#39;saveRule&#39;   => &#39;&#39;,
            );

置空$config里面的saveRule,上传后的名称为:59c8d38cdb968.jpg

若是感觉这种命名不可靠,可采取第二种方法:


$config = array(                &#39;maxSize&#39;    =>    3145728,
                &#39;rootPath&#39;   =>    &#39;./Uploads/&#39;,
                &#39;saveName&#39;   =>    array(&#39;uniqid&#39;, mt_rand(1,999999).&#39;_&#39;.md5(uniqid())),
                &#39;exts&#39;       =>    array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),
                &#39;autoSub&#39;    =>    true,
                &#39;subName&#39;    =>    array(&#39;date&#39;,&#39;Ymd&#39;),
            );

设置$config中: 'saveName' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())),
其最后的结果类似于:672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg

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

Atas ialah kandungan terperinci Thinkphp3.2中多文件上传只上传一张的问题解决. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn