php layui实现前端多图上传实例


Posted in PHP onJuly 30, 2019

php结合layui前端实现多图上传

前端html代码

<div class="layui-upload">

  <button type="button" class="layui-btn layui-btn-normal" id="testList">请选择图片</button>

  <span class="num_pic"></span>

  <div class="layui-upload-list">

    <table class="layui-table">

      <thead>

        <tr>

          <th>文件名</th>

          <th id="pic">图片预览</th>

          <th>大小</th>

          <th>状态</th>

          <th id="cao">操作</th>

        </tr>

      </thead>

      <tbody id="demoList"></tbody>

    </table>

  </div>

  <button type="button" class="layui-btn" id="testListAction">开始上传</button>

    <span class="num_pic"></span>

</div>

js 代码

<script type="text/javascript">

  layui.use('upload', function() {

    var $ = layui.jquery,

      upload = layui.upload;

    //多文件列表示例

    var demoListView = $('#demoList'),

      uploadListIns = upload.render({

        elem: '#testList',

        url: "{url('pic/index/upload')}",

        accept: 'images',

        acceptMime: 'image/*',

        size: 8192,

        multiple: true,

        number: 400,

        auto: false,

        exts: 'jpg|png|jpeg',

        bindAction: '#testListAction',

        choose: function(obj) {

          var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列

          //读取本地文件

          obj.preview(function(index, file, result) {

            var tr = $(['<tr id="upload-' + index + '">', '<td>' + file.name + '</td>', '<td><img src="' + result + '" alt="' + file.name + '" style="width: 100px;height: 40px;"></td>', '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>', '<td>等待上传</td>', '<td>', '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>', '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>', '</td>', '</tr>'].join(''));

            //单个重传

            tr.find('.demo-reload').on('click', function() {

              obj.upload(index, file);

              $("#upload-" + index).find("td").eq(2).html((file.size / 1014).toFixed(1) + 'kb');

            });

            //删除

            tr.find('.demo-delete').on('click', function() {

              delete files[index]; //删除对应的文件

              tr.remove();

              uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选

            });

            demoListView.append(tr);

            $(".num_pic").text("总共【" + demoListView.find("tr").length + "】张图片");

          });

        },

        done: function(res, index, upload) {

          if(res.code == 0) { //上传成功

            $("#cao").text("地址");

            var tr = demoListView.find('tr#upload-' + index),

              tds = tr.children();

            tds.eq(3).html('<span style="color: #5FB878;">上传成功</span>');

            tds.eq(4).html('<input type="text" name="imgs[]" value="' + res.file + '" class="layui-input" />'); //清空操作

            return delete this.files[index]; //删除文件队列已经上传成功的文件

          }

          this.error(index, upload);

        },

        allDone: function(obj) { //当文件全部被提交后,才触发

          layer.msg("上传文件数量:【" + obj.total + "】张,上传成功:【" + obj.successful + "】张,失败:【" + obj.aborted + "】", {

            time: 3000

          });

          console.log(obj.total); //得到总文件数

          console.log(obj.successful); //请求成功的文件数

          console.log(obj.aborted); //请求失败的文件数

        },

        error: function(index, upload) {

          var tr = demoListView.find('tr#upload-' + index),

            tds = tr.children();

          tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');

          tds.eq(4).find('.demo-reload').removeClass('layui-hide'); //显示重传

        }

      });

  });

</script>

后端代码

public function uploadAction(){

    $file=$_FILES['file'];

    $root_url = 'uploadfiles/pic/image/';

    if (!is_uploaded_file($file['tmp_name'])){

      $data = array('code'=>1,'msg'=>"错误");

      exit(json_encode($data,0));

    }

   /* $root_url.=date('Ymd').'/';*/

    $ext = pathinfo($file['name']);

    $num=makenum($this->memberinfo['id']);

    $root_url.=$num.'/';

    if (!is_dir($root_url)) {

      mkdir($root_url,0777, true);

    }

    $pa=file_list::get_file_list($root_url);

    $na=count($pa) + 1;

    if ($na<10){

      $name=$num.'-000'.$na;

    }elseif($na<100){

      $name=$num.'-00'.$na;

    }elseif($na<1000){

      $name=$num.'-0'.$na;

    }else{

      $name=$num.'-'.$na;

    }

    $n=$root_url.$name.".".$ext['extension'];

    $result=move_uploaded_file($file['tmp_name'],$n);

    if ($result){

      exit(json_encode(array("code"=>0,"msg"=>"ok","file"=>$n,"size"=>$file['size']),0));

    }else{

      exit(json_encode(array("code"=>1,"msg"=>"false","file"=>$n,"size"=>$file['size']),0));

    }

  }

上传效果:

php layui实现前端多图上传实例

php layui实现前端多图上传实例

以上就是php结合layui前端实现多图上传的全部知识点,感谢大家对三水点靠木的支持。

PHP 相关文章推荐
PHP新手上路(十二)
Oct 09 PHP
PHP中文件上传的一个问题
Sep 04 PHP
Wordpress 相册插件 NextGEN-Gallery 添加目录将中文转为拼音的解决办法
Dec 29 PHP
Destoon实现多表查询示例
Aug 21 PHP
Thinkphp中数据按分类嵌套循环实现方法
Oct 30 PHP
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
Nov 06 PHP
php中ob函数缓冲机制深入理解
Aug 03 PHP
Symfony模板的快捷变量用法实例
Mar 17 PHP
Smarty模板常见的简单应用分析
Nov 15 PHP
PHP批量修改文件名称的方法分析
Feb 27 PHP
PHP封装请求类实例分析【基于Yii框架】
Oct 17 PHP
详细分析PHP7与PHP5区别
Jun 26 PHP
安装docker和docker-compose实例详解
Jul 30 #PHP
docker-compose部署php项目实例详解
Jul 30 #PHP
php 使用mpdf实现指定字段配置字体样式的方法
Jul 29 #PHP
laradock环境docker-compose操作详解
Jul 29 #PHP
php 根据URL下载远程图片、压缩包、pdf等文件到本地
Jul 26 #PHP
PHP中散列密码的安全性分析
Jul 26 #PHP
PHP基于timestamp和nonce实现的防止重放攻击方案分析
Jul 26 #PHP
You might like
动画 《Pokemon Sword·Shield》系列WEB动画《薄明之翼》第2话声优阵容公开!
2020/03/06 日漫
在任意字符集下正常显示网页的方法二(续)
2007/04/01 PHP
如何在Ubuntu下启动Apache的Rewrite功能
2013/07/05 PHP
php使用curl访问https示例分享
2014/01/17 PHP
PHP实现对png图像进行缩放的方法(支持透明背景)
2015/07/15 PHP
PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)
2016/09/11 PHP
Laravel如何创建服务器提供者实例代码
2019/04/15 PHP
查看图片(前进后退)功能实现js代码
2013/04/24 Javascript
JavaScript字符串插入、删除、替换函数使用示例
2013/07/25 Javascript
JS画线(实例代码)
2013/11/20 Javascript
Seajs的学习笔记
2014/03/04 Javascript
NodeJS Express框架中处理404页面一个方式
2014/05/28 NodeJs
Jquery动态替换div内容及动态展示的方法
2015/01/23 Javascript
jQuery选择器总结之常用元素查找方法
2016/08/04 Javascript
jQuery绑定事件的四种方式介绍
2016/10/31 Javascript
JS实现延迟隐藏功能的方法(类似QQ头像鼠标放上展示信息)
2017/12/28 Javascript
小程序input数据双向绑定实现方法
2019/10/17 Javascript
vue-router定义元信息meta操作
2020/12/07 Vue.js
[00:48]食人魔魔法师至宝“金鹏之幸”全新模型和自定义特效展示
2019/12/19 DOTA
Python使用cx_Oracle调用Oracle存储过程的方法示例
2017/10/07 Python
python爬取m3u8连接的视频
2018/02/28 Python
python print 按逗号或空格分隔的方法
2018/05/02 Python
TensorFlow 模型载入方法汇总(小结)
2018/06/19 Python
python实现根据指定字符截取对应的行的内容方法
2018/10/23 Python
python scrapy爬虫代码及填坑
2019/08/12 Python
Python socket非阻塞模块应用示例
2019/09/12 Python
Python 解析简单的XML数据
2020/07/24 Python
Django自带的用户验证系统实现
2020/12/18 Python
CSS3中伪元素::before和::after的用法示例
2017/09/18 HTML / CSS
印度化妆品购物网站:Nykaa
2018/07/22 全球购物
如何用Lucene索引数据库
2016/02/23 面试题
自我反省检讨书
2014/01/23 职场文书
培训主管的职业生涯规划
2014/03/06 职场文书
总经理岗位职责
2015/02/04 职场文书
英语读书笔记
2015/07/02 职场文书
一篇文章弄懂MySQL查询语句的执行过程
2021/05/07 MySQL