layui Framework to Upload File and TP3.2. 3 of thinkPHP to Upload File Background Processing Operation Example
- 2021-09-24 21:51:18
- OfStack
This article describes the example of layui framework to upload files and TP 3.2. 3 to upload files for background processing operations. Share it for your reference, as follows:
The layui framework is version 1.0. 9. .
First, the html page code is as follows:
<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>
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 is being uploaded ');
layer.load();
},
success: function (msg) {
console.log(msg);
if(msg.msg=="success"){
layer.closeAll('loading');
layer.msg(" Upload succeeded ");
$("#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 php background value connection method:
# Upload file method
public function upload(){
$res=array(
'code'=>1,
'msg'=>'no sorry',
'data'=>array(
'src'=>'',
)
);
# Picture storage path
$directory = C('UPLOAD_PATH')."/Public/docment/";
# Determine whether the directory exists Create if it does not exist
if(!(is_dir($directory))){
$this->directory($directory);
}
# Get the last database 1 Article id As a 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);die;
}
/**
* Recursive file creation
* @author erwa<erwa@qingjinju.net>
*/
public function directory($dir){
return is_dir ( $dir ) or directory(dirname( $dir )) and mkdir ( $dir , 0777);
}
Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.