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

來源:互聯網
上載者:User
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'    =>    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

然,命名可根據需要自行修改,多檔案上傳方法很多,這裡只是提供個簡單便捷的方法!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.