您好,登錄后才能下訂單哦!
使用TP5框架怎么實現(xiàn)多圖上傳功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
1、效果圖(每點擊一次‘添加選項',就會有一個新的 file 框來添加新的圖片)
2、view
<!--不要忘了引入jquery文件--> <!-- post傳值方式和文件傳輸協(xié)議一定要加上 --> <input type="file" name="image[]"> <input type="button" id="add" name="add" value="+ 添加選項"> <button type="submit" name="submit">添加</button> <script type="text/javascript"> $("#add").click(function(){ $(this).before('<input type="file" name="image[]">'); }); </script>
3、controller
//接收從view來的圖片數(shù)組 $image=request()->file('image'); //實例化模型,并調(diào)用里面的添加圖片的方法 $details = new Details(); $info = $details->add($image); if($info === 1) { return '操作成功'; } else { return '操作失敗'; }
4、model
//將接收到的 $image foreach遍歷添加 foreach($image as $image) { //實例化模型 $details = new Details(); $time=date('Ymd',time()); //將當(dāng)前的時間戳定義為文件名 $filename=time(); //檢測是否存在存放圖片的文件夾 if(!file_exists(ROOT_PATH . 'public' . DS .'static'. DS .'img')) { //創(chuàng)建文件 mkdir(ROOT_PATH . 'public' . DS .'static'. DS .'img'); } //上傳圖片 $info=$image->move(ROOT_PATH . 'public' . DS .'static'. DS .'img'.DS.$time,$filename); //將圖片路徑存放在數(shù)據(jù)庫中 $details->url = $time.DS.$info->getFileName(); $details->allowField(true)->save(); } return 1;
看完上述內(nèi)容,你們掌握使用TP5框架怎么實現(xiàn)多圖上傳功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。