首頁  >  文章  >  後端開發  >  關於Thinkphp3.2簡單解決多檔案上傳以及只上傳一張的問題解析

關於Thinkphp3.2簡單解決多檔案上傳以及只上傳一張的問題解析

不言
不言原創
2018-06-08 13:42:191795瀏覽

下面要為大家帶來一篇Thinkphp3.2簡單解決多檔案上傳只上傳一張的問題。內容還挺不錯的,現在就分享給大家,也給大家做個參考。

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

#以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關建議:

thinkphp5上傳圖片及產生縮圖的方法

ThinkPHP3.2.3驗證碼的顯示和刷新以及校驗

THinkPHP取得客戶端IP與IP位址查詢的方法

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

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