溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用TP5框架怎么實現(xiàn)多圖上傳功能

發(fā)布時間:2021-04-13 15:41:23 來源:億速云 閱讀:130 作者:Leah 欄目:開發(fā)技術(shù)

使用TP5框架怎么實現(xiàn)多圖上傳功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

1、效果圖(每點擊一次‘添加選項',就會有一個新的 file 框來添加新的圖片)

使用TP5框架怎么實現(xiàn)多圖上傳功能

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è)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(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)容。

tp5
AI