zoukankan      html  css  js  c++  java
  • TP5之上传多张图片

    1、效果图(每点击一次‘添加选项’,就会有一个新的 file 框来添加新的图片)

    2、view

     1 <!--不要忘了引入jquery文件-->
     2 <!-- post传值方式和文件传输协议一定要加上 -->
     3 <input type="file" name="image[]">
     4 <input type="button" id="add" name="add" value="+ 添加选项">
     5 <button type="submit" name="submit">添加</button>
     6 
     7 <script type="text/javascript">
     8     $("#add").click(function(){
     9         $(this).before('<input type="file" name="image[]">');
    10     });
    11 </script>

    3、controller

     1 //接收从view来的图片数组
     2 $image=request()->file('image');
     3 
     4 //实例化模型,并调用里面的添加图片的方法
     5 $details = new Details();
     6 $info = $details->add($image);
     7 if($info === 1)
     8 {
     9     return '操作成功';
    10 }
    11 else
    12 {
    13     return '操作失败';
    14 }

    4、model

     1 //将接收到的 $image foreach遍历添加
     2 foreach($image as $image)
     3 {
     4     //实例化模型
     5     $details = new Details();
     6     $time=date('Ymd',time());
     7     //将当前的时间戳定义为文件名
     8     $filename=time();
     9     //检测是否存在存放图片的文件夹
    10     if(!file_exists(ROOT_PATH . 'public' . DS .'static'. DS .'img'))
    11     {
    12         //创建文件
    13         mkdir(ROOT_PATH . 'public' . DS .'static'. DS .'img');
    14     }
    15     //上传图片
    16     $info=$image->move(ROOT_PATH . 'public' . DS .'static'. DS .'img'.DS.$time,$filename);
    17     //将图片路径存放在数据库中
    18     $details->url = $time.DS.$info->getFileName();
    19     $details->allowField(true)->save();
    20 }
    21 return 1;

    over!over!over!

    let the world have no hard-to-write code ^-^
  • 相关阅读:
    bootstrap 基本页面元素,代码,列表
    Servlet 获取IllegelStateException
    Servlet RequestDispatcher需要注意的情况
    每一个Servlet只有一个实例,多个线程
    Servlet中进行context属性的同步
    servlet中的8个Listener
    Spring AOP切面的时候参数的传递
    C#中StreamReader读取中文时出现乱码问题总结
    开源 免费 java CMS
    測试设计及自己主动化全景图
  • 原文地址:https://www.cnblogs.com/ovim/p/10580322.html
Copyright © 2011-2022 走看看