layui框架實(shí)現(xiàn)文件上傳及TP3.2.3(thinkPHP)對(duì)上傳文件進(jìn)行后臺(tái)處理操作示例
本文實(shí)例講述了layui框架實(shí)現(xiàn)文件上傳及TP3.2.3對(duì)上傳文件進(jìn)行后臺(tái)處理操作。分享給大家供大家參考,具體如下:
layui框架是1.0.9版本。。
首先html頁(yè)面代碼如下:
<div class="layui-form-item" id="upload_file"> <div class="layui-input-block" style="width: 300px;"> <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>
接下來(lái)的php后臺(tái)接值的方法:
#上傳文件方法 public function upload(){ $res=array( 'code'=>1, 'msg'=>'no sorry', 'data'=>array( 'src'=>'', ) ); #圖片存放路徑 $directory = C('UPLOAD_PATH')."/Public/docment/"; #判斷目錄是否存在 不存在則創(chuàng)建 if(!(is_dir($directory))){ $this->directory($directory); } #獲取數(shù)據(jù)庫(kù)最后一條id 當(dāng)做文件名稱 $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; } /** * 遞歸創(chuàng)建文件 * @author erwa<erwa@qingjinju.net> */ public function directory($dir){ return is_dir ( $dir ) or directory(dirname( $dir )) and mkdir ( $dir , 0777); }
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)微信公眾號(hào)驗(yàn)證Token的示例代碼
這篇文章主要介紹了PHP實(shí)現(xiàn)微信公眾號(hào)驗(yàn)證Token的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12PHPCMS V9 添加二級(jí)導(dǎo)航的思路詳解
這篇文章主要介紹了PHPCMS V9 添加二級(jí)導(dǎo)航所遇到些問題,查詢導(dǎo)航欄的信息時(shí)返回的$r[arrchildid]與自己想象的不符,文檔上說(shuō)是返回子欄目id但是卻有些不同。下面小編通過分享本文給大家解答下2016-10-10PHP寫的簡(jiǎn)單數(shù)字驗(yàn)證碼實(shí)例
下面小編就為大家?guī)?lái)一篇PHP寫的簡(jiǎn)單數(shù)字驗(yàn)證碼實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-05-05php序列化函數(shù)serialize() 和 unserialize() 與原生函數(shù)對(duì)比
這篇文章主要介紹了php序列化函數(shù)serialize() 和 unserialize() 與php原生序列化方法對(duì)比,有需要的小伙伴可以參考下。2015-05-05IIS下配置頁(yè)面重寫(配合插件url-rewrite2去除頁(yè)面后綴名)的實(shí)現(xiàn)方法
這篇文章主要介紹了IIS下配置頁(yè)面重寫(配合插件url-rewrite2去除頁(yè)面后綴名)的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10PHP分頁(yè)初探 一個(gè)最簡(jiǎn)單的PHP分頁(yè)代碼的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇PHP分頁(yè)初探 一個(gè)最簡(jiǎn)單的PHP分頁(yè)代碼的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-06-06PHP安裝threads多線程擴(kuò)展基礎(chǔ)教程
php5.3或以上,且為線程安全版本。apache和php使用的編譯器必須一致,通過phpinfo()查看Thread Safety為enabled則為線程安全版,通過phpinfo()查看Compiler項(xiàng)可以知道使用的編譯器,本文給大家介紹PHP安裝threads多線程擴(kuò)展基礎(chǔ)教程,需要的朋友參考下2015-11-11