码迷,mamicode.com
首页 > Web开发 > 详细

thinkphp结合layui上传图片

时间:2018-07-23 11:05:16      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:orm   sage   use   上传文件   上传图片   var   sda   结合   int   

简单示例:

<script type="text/javascript">
layui.use([‘form‘, ‘layedit‘,‘element‘, ‘laydate‘,‘upload‘], function(){
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laysdate;
    // 上传
    var upload = layui.upload;
    var uploadInst1 = upload.render({
    elem: ‘#uploadImgBut1‘, //绑定元素
    url: "{:U(‘Admin/Upload/layuiupload‘)}",//上传接口
    done: function(res){
        if(res[‘state‘] ==1){
            layer.msg(res[‘message‘]);
            $("#uploadImg1").attr(‘src‘,res[‘path‘]).show();
            $("input#uploadImgSrc1").val(res[‘path‘]); 
        };
    }
    }); 
});
</script>

后台代码:

public function layuiupload(){
        $upload = new \Think\Upload();// 实例化上传类
        $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;// 设置附件上传大小
        $upload->exts      =     $imgType;// 设置附件上传类型
        $upload->rootPath  =     "./uploads/Picture/"; // 设置附件上传根目录
        $upload->savePath  =     ‘‘; // 设置附件上传(子)目录
        $data = array();
        $data[‘state‘] = 1;
        $data[‘message‘] = ‘上传成功‘;
        $data[‘path‘] = ‘‘;
        // 上传文件 
        $info = $upload->upload();
        if(!$info){
            $data[‘state‘] = 0;
            $data[‘message‘] =‘上传失败‘;
        };
        $path = "uploads/Picture/".$info[‘file‘][‘savepath‘].$info[‘file‘][‘savename‘];
        $data[‘path‘] = $path;
        echo json_encode($data);die;
    }

 

thinkphp结合layui上传图片

标签:orm   sage   use   上传文件   上传图片   var   sda   结合   int   

原文地址:https://www.cnblogs.com/e0yu/p/9353064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!