tp5使用layui实现多个图片上传(带附件选择)的方法实例
程序员文章站
2022-06-15 15:38:29
tp5使用layui实现多个图片上传(带附件选择),如何加载layui在此不详细说明,有需要可以百度html代码,主要处理都是在jq中,完成方法全部原创,也许不是最简单的,但也能实现效果{includ...
tp5使用layui实现多个图片上传(带附件选择),如何加载layui在此不详细说明,有需要可以百度
html代码,主要处理都是在jq中,完成方法全部原创,也许不是最简单的,但也能实现效果
{include file="public/header" /} <body> <div class="x-nav"> <span class="layui-breadcrumb"> <a href="">首页</a> <a href="">多选图片列表</a> <a> <cite>添加多选图片</cite></a> </span> <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" rel="external nofollow" title="刷新"> <i class="layui-icon" style="line-height:30px">ဂ</i></a> </div> <div class="x-body"> <div class="top" style="font-size: 14px">添加多选图片</div> <div style="width:100%;height: 5px;background-color: #077ee3;margin-top: 5px;margin-bottom: 20px"></div> <form action="" enctype="multipart/form-data" method="post" role="form" onsubmit="return check()"> <div class="layui-upload"> <button type="button" class="layui-btn" id="upload_img">多图片上传</button> <a class="layui-btn layui-btn-sm layui-btn-normal" onclick="selectimg('选择图片','{:url("selectimg")}','1000','600')" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <i class="layui-icon"></i>选择图片 </a> <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;"> 预览图: <div class="layui-upload-list" id="demo2"></div> </blockquote> </div> <input name="url" class="imginput" type="hidden"> <!--<button type="button" class="layui-btn" onclick="test()">--> <!--测试--> <!--</button>--> <div class="layui-form-item"> <label for="" class="layui-form-label"> </label> <button type="submit" class="layui-btn btnadd" lay-filter="add" lay-submit=""> 增加 </button> </div> </form> </div> <style> .imginput{ width: 600px; height: 35px; } .layui-form-label{ font-size: 14px; width: 100px; } select{ width: 500px; } #pre_img{ display: none; padding: 5px; border: 1px solid #999; } #demo2{ display: flex; display: -webkit-flex; /*justify-content: space-between;*/ flex-direction: row; flex-wrap: wrap; } .img{ width: 150px; height: 150px; } .btnadd{ margin-top: 40px; } .img_item{ display: flex; flex-direction: column; text-align: center; margin-right: 20px; margin-bottom: 20px; } .delimg{ text-align: center; line-height: 20px; width: 160px; height: 20px; background-color: red; color: white; margin-top: 5px; } </style> <script> function check(){ $('input[name="url"]').val(urllist); var str = $('input[name="url"]').val(); if(str ==''|| str==null || str=='undefined'){ alert("请选择图片"); return false; } } function selectimggo($url,$urlweb){ var index = 0; if(urllist.length>0){ index = urllist.length; } var img = $([ '<div class="img_item">', '<div class="img" style="overflow:hidden;">', '<img src="'+ $urlweb +'" alt="' + $url +'" class="layui-upload-img" style="max-width:150px;"></div>', '<div class="delimg" id="delimg" onclick=delimg("'+ index +'") href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" style="cursor:pointer ">删除</div></div>' ].join('')); $('#demo2').append(img); urllist.push($url); imglist.push($urlweb); } var imglist = []; var urllist = []; layui.use(['upload','jquery'],function () { $ = layui.jquery; var upload = layui.upload; //多图片上传 upload.render({ elem: '#upload_img' ,url:"{:url('share/upload_img')}" //上传接口 ,multiple: true ,before: function(obj){ //预读本地文件示例,不支持ie8 obj.preview(function(index, file, result){ }) } ,done: function(res){ var index0 = 0; if(urllist.length>0){ index0 = urllist.length; } var img0 = $([ '<div class="img_item">', '<div class="img" style="overflow:hidden;">', '<img src="'+ res.msg +'" class="layui-upload-img" style="max-width:150px;"></div>', '<div class="delimg" id="delimg" onclick=delimg("'+ index0 +'") href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" style="cursor:pointer ">删除</div></div>' ].join('')); $('#demo2').append(img0); urllist.push(res.url); imglist.push(res.msg); } ,error: function(){ // layer.close(layer.msg());//关闭上传提示窗口 //请求异常回调 } }); }); function delimg(index){ urllist.splice(index,1); imglist.splice(index,1); $('#demo2').empty(); for (var i=0;i<imglist.length;i++){ var img0 = $([ '<div class="img_item">', '<div class="img" style="overflow:hidden;">', '<img src="'+ imglist[i] +'" class="layui-upload-img" style="max-width:150px;"></div>', '<div class="delimg" id="delimg" onclick=delimg("'+ i +'") href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" style="cursor:pointer ">删除</div></div>' ].join('')); $('#demo2').append(img0); } } /*选择图片*/ function selectimg(title,url,w,h){ x_admin_show(title,url,w,h); } </script> </body> </html>
php代码
//php layui图片上传 public function upload_img(){ $file = request()->file('file'); // 获取上传的文件 if($file==null){ exit(json_encode(array('code'=>1,'msg'=>'未上传图片'))); }else{ //5、对上传文件做出条件限制(类型,大小等) $map = [ 'ext'=>'jpg,png,gif,jpeg',//后辍名 'size'=>320000000,//最大3m ]; //6、对上传的文件进行较验,如果合格就进行转移到预定设定好的public/uploads目录下 //返回保存的文件信息info,包括文件名和大小等数据 $info = $file->validate($map)->move(root_path . 'public/uploads/img'); //获取图片宽高 list( $width , $height , $type , $attr ) = getimagesize ($info->getpathname()); if(is_null($info)){ $this->error($info->geterror()); } $img = str_replace('\\','/',$info->getsavename()); //保存附件 $annexdata['filesize'] = $info->getinfo()['size']; $annexdata['mimetype'] = $info->getinfo()['type']; $annexdata['filename'] = $info->getinfo()['name']; $annexdata['imagewidth'] = $width; $annexdata['imageheight'] = $height; $annexdata['type'] = 'img'; $annexdata['url'] = $img; aannexmodel::create($annexdata); $img = constant("url")."/uploads/img/".$img; exit(json_encode(array('code'=>0,'msg'=>$img,'url'=>$annexdata['url']))); } } //多选图片添加页面 public function addimages(){ if($this->request->ispost()){ //2、获取提交过来的数据,最后true参数,表示连上传文件一起获取 $data = $this->request->param(true); $res = aimagesmodel::create($data); if(is_null($res)){ $this->error('文件添加失败'); }else{ $this->success('文件添加成功...',url('imageslist')); } return; } return $this->fetch('images_add'); }
到此这篇关于tp5使用layui实现多个图片上传(带附件选择)的方法实例的文章就介绍到这了,更多相关tp5 多图片上传内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: php去除数组中为0的元素的实例分析