This article mainly introduces the Layui framework implementation of file upload and TP3.2.3 to upload files background processing operations, combined with an example of the Layui framework combined with thinkphp file upload and processing operations related implementation skills, the need for friends can refer to the next
This paper describes the Layui framework to implement file upload and TP3.2.3 upload files for background processing operations. Share to everyone for your reference, as follows:
The Layui framework is version 1.0.9.
First the HTML page code is as follows:
<p class= "Layui-form-item" id= "Upload_file" > <p class= "Layui-input-block" style= "width:300px;" > <input type= "hidden" id= "Img_url1" name= "Headimageurl" value= ""/> <p class= "Layui-upload-drag "Id=" Uploadpic1 "lay-verify=" Uploadpic1 "> <p class=" layui-col-xs12 layui-col-md12 "> </p> <p class= "Button-hide" > <input type= "file" name = "Banner_file_upload" id= "Banner_file_upload" class= "Layui-uplaod-file" lay-type= "file" > </p> </p> </p></p>
The JS code is as follows:
<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 (' File Crossing '); Layer.load (); }, success:function (msg) { console.log (msg); if (msg.msg== "Success") { layer.closeall (' loading '); Layer.msg ("Upload success"); $ ("#img_url1"). attr ("value", MSG.SRC); } else if (msg.msg== "error") { layer.closeall (' loading '); Layer.msg (Msg.code); } , error:function (data) { layer.msg ("Upload failed"); Console.log (data);}); }); </script>
The next method of PHP backend value:
#上传文件方法public function upload () {$res =array (' Code ' =>1, ' msg ' = ' no sorry ', ' Data ' =>array ( ' src ' = ', '); #图片存放路径 $directory = C (' Upload_path '). " /public/docment/"; #判断目录是否存在 does not exist then create if (! ( Is_dir ($directory)) {$this->directory ($directory); } #获取数据库最后一条id as file name $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);d ie;} /*** creating a file recursively* @author erwa<erwa@qingjinju.net>*/public function directory ($dir) {return Is_dir ($dir) or directory (DirName ( $dir)) and mkdir ($dir, 0777);}