您好,登錄后才能下訂單哦!
這篇文章主要介紹layui框架怎么實現文件上傳及TP3.2.3對上傳文件進行后臺處理操作,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
layui框架是1.0.9版本。。
首先html頁面代碼如下:
<div class="layui-form-item" id="upload_file"> <div class="layui-input-block" > <input type="hidden" id="img_url1" name="HeadImageUrl" value=""/> <div class="layui-upload-drag" id="uploadpic1" lay-verify="uploadpic1"> <div class="layui-col-xs12 layui-col-md12"> <img class="layui-upload-img" id="demo1" > </div> <div class="button-hide"> <input type="file" name="banner_file_upload" id="banner_file_upload" class="layui-uplaod-file" lay-type="file"> </div> </div> </div> </div>
js代碼如下:
<script type="text/javascript" th:inline="javascript"> layui.use('upload', function (){ var upload = layui.upload; var url="__PUBLIC__"; upload({ elem: '#banner_file_upload', url: "/index.php/Admin/Product/upload", method: 'post', before: function(obj){ console.log('文件上傳中'); layer.load(); }, success: function (msg) { console.log(msg); if(msg.msg=="success"){ layer.closeAll('loading'); layer.msg("上傳成功"); $("#img_url1").attr("value", msg.src); }else if(msg.msg=="error"){ layer.closeAll('loading'); layer.msg(msg.code); } }, error:function (data) { layer.msg("上傳失敗"); console.log(data); } }); }); </script>
接下來的php后臺接值的方法:
#上傳文件方法 public function upload(){ $res=array( 'code'=>1, 'msg'=>'no sorry', 'data'=>array( 'src'=>'', ) ); #圖片存放路徑 $directory = C('UPLOAD_PATH')."/Public/docment/"; #判斷目錄是否存在 不存在則創建 if(!(is_dir($directory))){ $this->directory($directory); } #獲取數據庫最后一條id 當做文件名稱 $product_last_id=D('ApiProduct')->getLastId(); $savename="ApiProduct_".time().'_'.($product_last_id['id']+1); $upload = new \Think\Upload(); $upload->maxSize = 0; $upload->exts = array('doc','docx','xls','xlsx','pdf','txt'); $upload->rootPath = $directory; $upload->saveName="$savename"; $upload->savePath = ''; $info = $upload->uploadOne($_FILES['banner_file_upload']); if(!$info){ $res['code']=$upload->getError(); $res['msg']='error'; }else{ $res['code']=0; $res['msg']='success'; $res['src']="/Public/docment/".$savename.".".$info['ext']; } echo json_encode($res);die; } /** * 遞歸創建文件 * @author erwa<erwa@qingjinju.net> */ public function directory($dir){ return is_dir ( $dir ) or directory(dirname( $dir )) and mkdir ( $dir , 0777); }
以上是“layui框架怎么實現文件上傳及TP3.2.3對上傳文件進行后臺處理操作”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。