Simple example:
<script type= "Text/javascript" >Layui.use ([' Form ', ' layedit ', ' element ', ' laydate ', ' upload ',function(){ varform =Layui.form; varLayer =Layui.layer; varLayedit =Layui.layedit; varLaydate =layui.laysdate; //Upload varUpload =Layui.upload; varUploadInst1 =Upload.render ({elem:' #uploadImgBut1 ',//binding ElementsURL: "{: U (' Admin/upload/layuiupload ')}",//Upload InterfaceDonefunction(res) {if(res[' state ') ==1) {layer.msg (res[' Message ']); $("#uploadImg1"). attr (' src ', res[' path ']). Show (); $("Input#uploadimgsrc1"). Val (res[' path ']); }; } }); });</script>
Background code:
Public functionlayuiupload () {$upload=New\think\upload ();//instantiating an upload class $imgSize=intval(CP (' imgsize ')); $imgType= CP (' Imgtype '); $imgSize= !Empty($imgSize) ?$imgSize: 3145728; $imgType= !Empty($imgType) ?Explode(‘,‘,$imgType) :Array(' jpg ', ' gif ', ' PNG ', ' JPEG '); $upload->maxsize =$imgSize;//set attachment upload size $upload->exts =$imgType;//set attachment upload type $upload->rootpath = "./uploads/picture/";//Set attachments upload root directory $upload->savepath = ";//Set attachments Upload (sub) directory $data=Array(); $data[' state '] = 1; $data[' message '] = ' upload succeeded '; $data[' path '] = '; //Uploading Files $info=$upload-upload (); if(!$info){ $data[' state '] = 0; $data[' message '] = ' upload failed '; }; $path= "Uploads/picture/".$info[' File '] [' Savepath '].$info[' File '] [' Savename ']; $data[' path '] =$path; EchoJson_encode ($data); die; }
thinkphp combined Layui upload image