欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TP5框架實現(xiàn)上傳多張圖片的方法分析

 更新時間:2020年03月29日 09:48:21   作者:Anwug!  
這篇文章主要介紹了TP5框架實現(xiàn)上傳多張圖片的方法,結(jié)合實例形式分析了TP5上傳多張圖片相關(guān)的視圖、模型、控制器操作技巧,需要的朋友可以參考下

本文實例講述了TP5框架實現(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());
  //將當前的時間戳定義為文件名
  $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;

5、over over over

更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論