PbootCMS通過layui上傳實(shí)現(xiàn)留言文件上傳功能
話不多說,直接上圖上代碼–這里以上傳圖片為例!其他文件自行修改
首先引入layui框架
<link rel="stylesheet" href="{pboot:sitetplpath}/layui/css/layui.css?v=v2.5.4"> <script type="text/javascript" src="{pboot:sitetplpath}/layui/layui.all.js?v=v2.5.4"></script>
然后修改前端的留言頁面
<div class="form-group"> <label for="mobile">頭 像</label> <div> <input type="text" name="ico" id="ico" placeholder="請上傳縮略圖" class="layui-input"> <button type="button" class="layui-btn upload" data-des="ico"> <i class="layui-icon"></i>上傳圖片 </button> <div id="ico_box"></div> </div> </div> <script> layui.use(['element','upload'], function(){ var element = layui.element; var upload = layui.upload; //執(zhí)行單圖片實(shí)例 var uploadInst = upload.render({ elem: '.upload' //綁定元素 ,url: '/index.php?p=/index/upload' //上傳接口 ,field: 'upload' //字段名稱 ,multiple: false //多文件上傳 ,accept: 'images' //接收文件類型 images(圖片)、file(所有文件)、video(視頻)、audio(音頻) ,acceptMime: 'image/*' ,done: function(res){ var item = this.item; layer.closeAll('loading'); //關(guān)閉loading if(res.code==1){ $('#ico').val(res.data[0]); $('#ico_box').html("<img src='"+res.data[0]+"' width=80 >"); layer.msg('上傳成功!'); }else{ layer.msg('上傳失敗:'+res.data); } } ,error: function(){ layer.closeAll('loading'); //關(guān)閉loading layer.msg('上傳發(fā)生錯誤!'); } }); }); </script>
打開apps/home/controller/IndexController.php
新增上傳入口函數(shù)
public function upload() { $upload = upload('upload'); if (is_array($upload)) { json(1, $upload); } else { json(0, $upload); } }
后臺對應(yīng)的改成圖片展示或者其他形式
注意:前端上傳功能會影響網(wǎng)站的安全性,容易導(dǎo)致網(wǎng)站被入侵,如非必須情況不建議使用此功能。