thinkphp5多文件上傳如何實現
一、總結
一句話總結:官方文檔,測試一下,一定要測試,打印中間變量,就知道每句話是什么意思,一定要測試一下。又簡單有快。
測試一下,你就能確定中間變量和你的是不是一樣,你是不是可以照着他的方法弄
1、框架或者插件遇到不懂的語句而又一定要用怎么辦?
測試,打印中間變量,就比較輕松了
2、單文件上傳和多文件上傳混合在一起的上傳怎么解決?
單文件上傳和多文件上傳混合在一起(循環解決),多文件上傳部分單獨處理(選擇結構)
照着手冊整,可以省好多事
不懂的語句打印中間結果
1 //2、獲取傳入的文件數據 2 foreach ($_FILES as $key=>$val){ 3 if($_FILES[$key]['tmp_name']){ 4 // 獲取表單上傳文件 例如上傳了001.jpg 5 $file = request()->file($key); 6 if($key=='a_content_pic3'){//多圖上傳的情況 7 //不用拆,官方文檔有怎么用 8 $files=$file; 9 $data[$key]=null; 10 foreach($files as $perFile){ 11 // 移動到框架應用根目錄/public/uploads/ 目錄下 12 $info = $perFile->move(ROOT_PATH . 'public' . DS . 'static/uploads/student/note'."/{$jieduanStr}"); 13 $data[$key].='/static/uploads/student/note/'."{$jieduanStr}/".$info->getSaveName().','; 14 } 15 }else{ 16 // 移動到框架應用根目錄/public/uploads/ 目錄下 17 $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads/student/note'."/{$jieduanStr}"); 18 // 已經上傳成功,我們要把文件的路徑寫進數據庫 19 $data[$key]='/static/uploads/student/note/'."{$jieduanStr}/".$info->getSaveName(); 20 } 21 } 22 }
二、thinkphp5多文件上傳如何實現
1、多文件上傳代碼
如果你使用的是多文件上傳表單,例如:
<form action="/index/index/upload" enctype="multipart/form-data" method="post"> <input type="file" name="image[]" /> <br> <input type="file" name="image[]" /> <br> <input type="file" name="image[]" /> <br> <input type="submit" value="上傳" /> </form>
控制器代碼可以改成:
public function upload(){ // 獲取表單上傳文件 $files = request()->file('image'); foreach($files as $file){ // 移動到框架應用根目錄/public/uploads/ 目錄下 $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'); if($info){ // 成功上傳后 獲取上傳信息 // 輸出 jpg echo $info->getExtension(); // 輸出 42a79759f284b767dfcb2a0197904287.jpg echo $info->getFilename(); }else{ // 上傳失敗獲取錯誤信息 echo $file->getError(); } } }
上傳界面如下:

2、上傳過程中dump($_FILES);的值如下

3、上傳成功的返回信息如下
其實也就是下面代碼的輸出結果
echo $info->getExtension(); echo "<br>"; // 輸出 42a79759f284b767dfcb2a0197904287.jpg echo $info->getFilename(); echo "<br>";

三、復雜結構數據的多文件上傳
1、上傳過程中部分dump($_FILES);的值如下

結構特點是單文件上傳和多文件上傳混合在一起(循環解決),多文件上傳部分單獨處理(選擇結構)
2、核心代碼
1 //2、獲取傳入的文件數據 2 foreach ($_FILES as $key=>$val){ 3 if($_FILES[$key]['tmp_name']){ 4 // 獲取表單上傳文件 例如上傳了001.jpg 5 $file = request()->file($key); 6 if($key=='a_content_pic3'){//多圖上傳的情況 7 //不用拆,官方文檔有怎么用 8 $files=$file; 9 $data[$key]=null; 10 foreach($files as $perFile){ 11 // 移動到框架應用根目錄/public/uploads/ 目錄下 12 $info = $perFile->move(ROOT_PATH . 'public' . DS . 'static/uploads/student/note'."/{$jieduanStr}"); 13 $data[$key].='/static/uploads/student/note/'."{$jieduanStr}/".$info->getSaveName().','; 14 } 15 }else{ 16 // 移動到框架應用根目錄/public/uploads/ 目錄下 17 $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads/student/note'."/{$jieduanStr}"); 18 // 已經上傳成功,我們要把文件的路徑寫進數據庫 19 $data[$key]='/static/uploads/student/note/'."{$jieduanStr}/".$info->getSaveName(); 20 } 21 } 22 }
3、上傳好了的結果數據
1 array(11) { 2 ["a_type"] => string(1) "2" 3 ["a_title"] => string(5) "33333" 4 ["a_brief"] => string(11) "33333333333" 5 ["a_content_art1"] => string(0) "" 6 ["a_create_time"] => int(1535563492) 7 ["a_update_time"] => int(1535563492) 8 ["a_authorid"] => string(2) "33" 9 ["a_g_id"] => int(17) 10 ["a_jieduan_id"] => string(1) "1" 11 ["a_picture"] => string(81) "/static/uploads/student/note/engage/20180830\dd2c9ae77b8c2c1f764d98ee86dcfb71.png" 12 ["a_content_pic3"] => string(164) "/static/uploads/student/note/engage/20180830\f972fe240bc0d315ae0e3f5fadd31486.jpg,/static/uploads/student/note/engage/20180830\71646f316808c0cd057813086c76554c.jpg," 13 }
