1. 程式人生 > >(13)學習tp5之上傳圖片

(13)學習tp5之上傳圖片

系統學習tp5框架,請點選下面的連線


總結:

1、多圖上傳,name值要是個陣列。控制器中直接遍歷

2、圖片上傳思路:表單提交後,接收-》效驗-》移動檔案-》獲取上傳後的路徑-》入庫

路由:

use think\Route;
//該檔案是路由的配置檔案,解決複雜場景用的
Route::get('index','index/VaData/index');
Route::post('upload','index/VaData/upload');  //單圖
Route::post('manyUpload','index/VaData/manyUpload');  //多圖

模板程式碼:


控制器讀取的配置檔案:


控制器程式碼:

public function 
index(){ return $this->fetch(); } //單圖 public function upload(){ $file = $this->request->file('img'); //接收圖片 //dump($file);die; //驗證規則等,在配置檔案中讀取(多維用點或['']讀取) $info = $file->validate(Config::get('fileUpload.file_config'))     ->move(Config::get('fileUpload.file_path')); if($info){
// 成功上傳後 獲取上傳資訊 echo $info->getExtension(); //上傳檔案的字尾 echo $info->getSaveName(); //上傳後儲存的路徑 echo $info->getFilename(); //上傳後的新名稱 }else{ // 上傳失敗獲取錯誤資訊 echo $file->getError(); } } //多圖 public function manyUpload(){ $files = $this->request->file('image'); //接收圖片 //dump($files);die; foreach($files
as $file) { //多圖上傳關鍵在這 //驗證規則等,在配置檔案中讀取 $info = $file->validate(Config::get('fileUpload.file_config')) ->move(Config::get('fileUpload.file_path')); if ($info) { // 成功上傳後 獲取上傳資訊 echo $info->getExtension(); //上傳檔案的字尾 echo "<br />"; echo $info->getSaveName(); //上傳後儲存的路徑 echo "<br />"; echo $info->getFilename(); //上傳後的新名稱 echo "<br />"; } else { // 上傳失敗獲取錯誤資訊 echo $file->getError(); } } }