首頁 >後端開發 >php教程 >Thinkphp3.2多檔上傳只上傳一張的問題解決

Thinkphp3.2多檔上傳只上傳一張的問題解決

黄舟
黄舟原創
2017-09-26 09:08:191400瀏覽

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

####y,命名可依需要自行修改,這裡只是提供很多簡單便捷的方法! ###

以上是Thinkphp3.2多檔上傳只上傳一張的問題解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn