PHP combined with layui front-end to implement multiple image uploads

藏色散人
Release: 2023-04-07 06:40:01
forward
4677 people have browsed it

php combined with layui front-end to realize multi-image upload

Front-end html code

<div class="layui-upload">
    <button type="button" class="layui-btn layui-btn-normal" id="testList">请选择图片</button>
    <span class="num_pic"></span>
    <div class="layui-upload-list">
        <table class="layui-table">
            <thead>
                <tr>
                    <th>文件名</th>
                    <th id="pic">图片预览</th>
                    <th>大小</th>
                    <th>状态</th>
                    <th id="cao">操作</th>
                </tr>
            </thead>
            <tbody id="demoList"></tbody>
        </table>
    </div>
    <button type="button" class="layui-btn" id="testListAction">开始上传</button>
        <span class="num_pic"></span>
</div>
Copy after login

js code

<script type="text/javascript">
    layui.use(&#39;upload&#39;, function() {
        var $ = layui.jquery,
            upload = layui.upload;
        //多文件列表示例
        var demoListView = $(&#39;#demoList&#39;),
            uploadListIns = upload.render({
                elem: &#39;#testList&#39;,
                url: "{url(&#39;pic/index/upload&#39;)}",
                accept: &#39;images&#39;,
                acceptMime: &#39;image/*&#39;,
                size: 8192,
                multiple: true,
                number: 400,
                auto: false,
                exts: &#39;jpg|png|jpeg&#39;,
                bindAction: &#39;#testListAction&#39;,
                choose: function(obj) {
                    var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                    //读取本地文件
                    obj.preview(function(index, file, result) {
                        var tr = $([&#39;<tr id="upload-&#39; + index + &#39;">&#39;, &#39;<td>&#39; + file.name + &#39;</td>&#39;, &#39;<td><img src="&#39; + result + &#39;" alt="&#39; + file.name + &#39;"   style="max-width:90%"></td>&#39;, &#39;<td>&#39; + (file.size / 1014).toFixed(1) + &#39;kb</td>&#39;, &#39;<td>等待上传</td>&#39;, &#39;<td>&#39;, &#39;<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>&#39;, &#39;<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>&#39;, &#39;</td>&#39;, &#39;</tr>&#39;].join(&#39;&#39;));
                        //单个重传
                        tr.find(&#39;.demo-reload&#39;).on(&#39;click&#39;, function() {
                            obj.upload(index, file);
                            $("#upload-" + index).find("td").eq(2).html((file.size / 1014).toFixed(1) + &#39;kb&#39;);
                        });
                        //删除
                        tr.find(&#39;.demo-delete&#39;).on(&#39;click&#39;, function() {
                            delete files[index]; //删除对应的文件
                            tr.remove();
                            uploadListIns.config.elem.next()[0].value = &#39;&#39;; //清空 input file 值,以免删除后出现同名文件不可选
                        });
                        demoListView.append(tr);
                        $(".num_pic").text("总共【" + demoListView.find("tr").length + "】张图片");
                    });
                },
                done: function(res, index, upload) {
                    if(res.code == 0) { //上传成功
                        $("#cao").text("地址");
                        var tr = demoListView.find(&#39;tr#upload-&#39; + index),
                            tds = tr.children();
                        tds.eq(3).html(&#39;<span style="color: #5FB878;">上传成功</span>&#39;);
                        tds.eq(4).html(&#39;<input type="text" name="imgs[]"  value="&#39; + res.file + &#39;" class="layui-input" />&#39;); //清空操作
                        return delete this.files[index]; //删除文件队列已经上传成功的文件
                    }
                    this.error(index, upload);
                },
                allDone: function(obj) { //当文件全部被提交后,才触发
                    layer.msg("上传文件数量:【" + obj.total + "】张,上传成功:【" + obj.successful + "】张,失败:【" + obj.aborted + "】", {
                        time: 3000
                    });
                    console.log(obj.total); //得到总文件数
                    console.log(obj.successful); //请求成功的文件数
                    console.log(obj.aborted); //请求失败的文件数
                },
                error: function(index, upload) {
                    var tr = demoListView.find(&#39;tr#upload-&#39; + index),
                        tds = tr.children();
                    tds.eq(2).html(&#39;<span style="color: #FF5722;">上传失败</span>&#39;);
                    tds.eq(4).find(&#39;.demo-reload&#39;).removeClass(&#39;layui-hide&#39;); //显示重传
                }
            });
    });
</script>
Copy after login

Backend code

public function uploadAction(){
        $file=$_FILES[&#39;file&#39;];
        $root_url =  &#39;uploadfiles/pic/image/&#39;;
        if (!is_uploaded_file($file[&#39;tmp_name&#39;])){
            $data = array(&#39;code&#39;=>1,&#39;msg&#39;=>"错误");
            exit(json_encode($data,0));
        }
      /*  $root_url.=date(&#39;Ymd&#39;).&#39;/&#39;;*/
        $ext = pathinfo($file[&#39;name&#39;]);
        $num=makenum($this->memberinfo[&#39;id&#39;]);
        $root_url.=$num.&#39;/&#39;;
        if (!is_dir($root_url)) {
            mkdir($root_url,0777, true);
        }
        $pa=file_list::get_file_list($root_url);
        $na=count($pa) + 1;
        if ($na<10){
            $name=$num.&#39;-000&#39;.$na;
        }elseif($na<100){
            $name=$num.&#39;-00&#39;.$na;
        }elseif($na<1000){
            $name=$num.&#39;-0&#39;.$na;
        }else{
            $name=$num.&#39;-&#39;.$na;
        }
        $n=$root_url.$name.".".$ext[&#39;extension&#39;];
        $result=move_uploaded_file($file[&#39;tmp_name&#39;],$n);
        if ($result){
            exit(json_encode(array("code"=>0,"msg"=>"ok","file"=>$n,"size"=>$file[&#39;size&#39;]),0));
        }else{
            exit(json_encode(array("code"=>1,"msg"=>"false","file"=>$n,"size"=>$file[&#39;size&#39;]),0));
        }
    }
Copy after login

Upload effect:

PHP combined with layui front-end to implement multiple image uploadsPHP combined with layui front-end to implement multiple image uploads

##

The above is the detailed content of PHP combined with layui front-end to implement multiple image uploads. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template