Home  >  Article  >  Backend Development  >  How to use php and layui to implement image upload and preview code

How to use php and layui to implement image upload and preview code

不言
不言Original
2018-07-27 10:39:114184browse

This article introduces you to the code about how to use PHP and layui to upload and preview images. It has a good reference value and I hope it can help friends in need.

End code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>upload模块快速使用</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>
    
    <button type="button" class="layui-btn" id="test1">
      <i class="layui-icon">&#xe67c;</i>上传图片
    </button>
    <div class="layui-upload-list">
        <img class="layui-upload-img" width="100px" height="100px" id="demo1">
        <p id="demoText"></p>
    </div>

    <script src="/layui/layui.js"></script>
    <script>

        layui.use(&#39;upload&#39;, function(){
            var $ = layui.jquery
            ,layer = layui.layer
            ,upload = layui.upload;

            var uploadInst = upload.render({
                elem:&#39;#test1&#39;
                ,url:&#39;/index/upload/upload&#39;
                ,accept:&#39;file&#39;  // 允许上传的文件类型
                ,auto:true // 自动上传
                ,before:function (obj) {
                    // console.log(obj);
                    // 预览
                    obj.preview(function(index,file,result) {
                        // console.log(file.name);    //图片地址
                        // console.log(file.type);    //图片格式
                        // console.log(file.size);    //图片大小
                        // console.log(file);    //图片地址
                        $(&#39;#demo1&#39;).attr(&#39;src&#39;,result); //图片链接 base64
                    });
                }
                // 上传成功回调
                ,done:function(res) {
                    // console.log(upload);
                    console.log(res);
                    if (res.code == 0) {
                        layer.msg(res.msg);
                    }else{
                        layer.msg(res.msg);
                    }
                }
                // 上传失败回调
                ,error:function(index,upload) {
                    // 上传失败
                }
            });

        })
    </script>
</body>
</html>

php Backend:

public function upload()
    {
        
        $file = request()->file(&#39;file&#39;);
        $info = $file->move(ROOT_PATH . &#39;public&#39; . DS . &#39;uploads&#39;);
        if($info) {
            $data[&#39;code&#39;] = 0;
            $data[&#39;msg&#39;] = $info->getSaveName();
        }else{
            $data[&#39;code&#39;] = 1;
            $data[&#39;msg&#39;] = $file->getError();
        }
            return json($data);
    }

Related recommendations:

How does php achieve equal-proportion compression of images?

How does php code in excel files? Converted? How to use php encoding conversion

The above is the detailed content of How to use php and layui to implement image upload and preview code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to encrypt the php IDNext article:How to encrypt the php ID