Rumah > pembangunan bahagian belakang > tutorial php > thinkphp 下多图ajax上传图片

thinkphp 下多图ajax上传图片

不言
Lepaskan: 2023-04-02 19:58:01
asal
1974 orang telah melayarinya

这篇文章主要介绍了关于thinkphp 下多图ajax上传图片,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

碰到一个项目,有一个比较繁琐的功能6个ajax上传,基本上每个上传逻辑多不一样,记录一下

thinkphp的view页面:

id方便找到这个元素 name一定要加 [ ]

<div class="btns">
  <a href="javascript:;" class="a-upload">
    <input type="file" id="fileaq" name="fileaq[]" data-filesType="words" class="uploadInput" multiple="multiple" />
    <i class="iconfont icon-shangchuan"></i>上传附件
  </a>
  <a href="javascript:void(0)" class="submit" id="aq_sub">发布</a>
</div>
Salin selepas log masuk

点击发布先判定一下,然后在把需要的参数传递给doUploadFiles函数

    //发布案情
        $(&#39;#aq_sub&#39;).click(function() {            
        var guanxi = &#39;many_one&#39;;            
        var type_file = &#39;file&#39;;            
        var type_name = &#39;fileaq&#39;;            
        var anqing = $(&#39;#anqing&#39;).val();            
        if ($.trim(anqing).length == 0) {
                layer.alert(&#39;请输入内容!\n&#39;);
                $(&#39;#anqing&#39;).focus();                
                return false;
            } else {                
            var cate_id = 3;
                doUploadFiles(cate_id, type_file, type_name, guanxi, anqing);
            }
        })
Salin selepas log masuk

参数说明

cate_id:多个上传的识别id

type_file: 判断是图片还是文件上传(nofiley:有的文件不需要上传

type_name:上传文件的id

guanxi:关系 项目需要 参数分为many_one,many_many,one_one(多个用户每人一条数据,多个用户每人多条数据,多个用户一人一条数据)

content:内容

function doUploadFiles(cate_id, type_file, type_name, guanxi, content) {        
var guanxi = arguments[3] ? arguments[3] : &#39;many_one&#39;; //设置关系
        var formData = new FormData();        
        var fangchan_id = $(&#39;#fangchan_id&#39;).val();
        formData.append("fangchan_id", fangchan_id);
        formData.append("cate_id", cate_id);
        formData.append("guanxi", guanxi);
        formData.append("content", content);        
        if(type_file !=&#39;nofile&#39;){
            formData.append("type_file", type_file);
            formData.append("file_length", $("#"+type_name)[0].files.length);            
            for(var i=0; i<$("#"+type_name)[0].files.length;i++){
                formData.append(&#39;file[]&#39;,$("#"+type_name)[0].files[i]);
            }
        }

        $.ajax({
            url: &#39;/Property/jindiaoHandle&#39;,
            type: &#39;POST&#39;,
            data: formData,
            dataType: "json",
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
                console.log(&#39;上传:&#39;,data)                
                if (data.status == 200) {
                    layer.msg(data.msg, { icon: 1 });
                    window.location.reload();
                } else {
                    layer.msg(data.msg, { icon: 1 });                    
                    return false;
                }

            }

        });

    }
Salin selepas log masuk

php代码比较长

/**
     * 提交房源尽调
     */
    public function jindiaoHandle()
    {
        $user_id = session(&#39;user_id&#39;);
        $fangchan_id = I(&#39;post.fangchan_id&#39;);
        $cate_id = I(&#39;post.cate_id&#39;);
        $cate_arr = array(&#39;6&#39;,&#39;7&#39;,&#39;8&#39;);
        $content = I(&#39;post.content&#39;);
        $type_file = I(&#39;post.type_file&#39;);
        $file_length = I(&#39;post.file_length&#39;);   //判断是否上传文件
        //many_one  多个用户存在一条   many_many 多个用户存在多条    one_one 只能催在一条数据
        $guanxi = I(&#39;post.guanxi&#39;);
        $guanxi?$guanxi:&#39;many_one&#39;;
        $content?$content:&#39;0&#39;;
        if(empty($user_id)){
            $ret = [&#39;status&#39; => &#39;1001&#39;, &#39;msg&#39; => &#39;请先登录!&#39;.$user_id, &#39;data&#39; => &#39;&#39;];
            $this->ajaxReturn($ret, &#39;json&#39;);
        }else{
            $level = M(&#39;users&#39;)->where([&#39;user_id&#39; => $user_id])->getField(&#39;level&#39;);
            //判断是不是法拍经理
            if ($level != 2) {
                $ret = [&#39;status&#39; => &#39;1002&#39;, &#39;msg&#39; => &#39;您没有权限填写!&#39;, &#39;data&#39; => &#39;&#39;];
                $this->ajaxReturn($ret);
            }
        }
        if(empty($fangchan_id))
        {
            $ret = [&#39;status&#39; => &#39;1003&#39;, &#39;msg&#39; => &#39;找不到此房源!&#39;, &#39;data&#39; => &#39;&#39;];
            $this->ajaxReturn($ret);
        }
        if(empty($cate_id))
        {
            $ret = [&#39;status&#39; => &#39;1004&#39;, &#39;msg&#39; => &#39;找不到此尽调类型!&#39;, &#39;data&#39; => &#39;&#39;];
            $this->ajaxReturn($ret);
        }
        if(empty($content))
        {
            $ret = [&#39;status&#39; => &#39;1005&#39;, &#39;msg&#39; => &#39;内容不能为空!&#39;, &#39;data&#39; => &#39;&#39;];
            $this->ajaxReturn($ret);

        }

        $fc_user_id = M(&#39;fangchan&#39;)->where([&#39;fangchan_id&#39; => $fangchan_id])->getField(&#39;user_id&#39;);
        //判断是不是该房产的法拍经理
        if ($fc_user_id == $user_id) {
            $data = [
                &#39;fangchan_id&#39; => $fangchan_id,
                &#39;user_id&#39;     => $user_id,
                &#39;cate_id&#39;     => $cate_id,
                &#39;content&#39;     => $content,
                &#39;res_num&#39;     => $file_length,
                &#39;add_time&#39;    => time(),
                &#39;is_user&#39;     => 1,
                &#39;is_show&#39;     => &#39;1&#39;,
            ];
        } else {
            if(!in_array($cate_id,$cate_arr))
            {
                $fc_add_time = M(&#39;fangchan&#39;)->where([&#39;fangchan_id&#39; => $fangchan_id])->getField(&#39;add_time&#39;); //获取添加时间
                if ((time() - $fc_add_time) < (12 * 60 * 60)) {
                    $arr = [&#39;status&#39; => &#39;1006&#39;, &#39;msg&#39; => &#39;请于24小时候后来发布!&#39;, &#39;data&#39; => &#39;&#39;];
                    $this->ajaxReturn($arr, &#39;json&#39;);
                }
            }
            $data = [
                &#39;fangchan_id&#39; => $fangchan_id,
                &#39;user_id&#39;     => $user_id,
                &#39;cate_id&#39;     => $cate_id,
                &#39;content&#39;     => $content,
                &#39;res_num&#39;     => $file_length,
                &#39;add_time&#39;    => time(),
                &#39;is_user&#39;    => 0,
                &#39;is_show&#39;     => &#39;1&#39;,
            ];
        }

        if($guanxi==&#39;many_one&#39;)
        {
            $fc_jindiao_data = M(&#39;fangchan_jindiao&#39;)
                ->where([&#39;fangchan_id&#39;=>$fangchan_id,&#39;user_id&#39;=>$user_id,&#39;cate_id&#39;=>$cate_id])
                ->getField(&#39;jindiao_id&#39;);
            //判断房产尽调是修改还是添加
            if($fc_jindiao_data){
                $res_edit = M(&#39;fangchan_jindiao&#39;)->where(&#39;jindiao_id=&#39;.$fc_jindiao_data)->save($data);
            }else{
                $res_add = M(&#39;fangchan_jindiao&#39;)->add($data);
            }
        }elseif($guanxi==&#39;one_one&#39;)
        {
            $fc_jindiao_data = M(&#39;fangchan_jindiao&#39;)
                ->where([&#39;fangchan_id&#39;=>$fangchan_id,&#39;cate_id&#39;=>$cate_id])
                ->getField(&#39;jindiao_id&#39;);
            //判断房产尽调是修改还是添加
            if($fc_jindiao_data){
                $res_edit = M(&#39;fangchan_jindiao&#39;)->where(&#39;jindiao_id=&#39;.$fc_jindiao_data)->save($data);
            }else{
                $res_add = M(&#39;fangchan_jindiao&#39;)->add($data);
            }
        }elseif($guanxi==&#39;many_many&#39;)
        {
            $res_add = M(&#39;fangchan_jindiao&#39;)->add($data);
        }


        //判断是否有文件
        if(!empty($file_length) || $file_length!=0)
        {
            $result = self::uploadFile($type_file);
            if($result[&#39;status&#39;] == -1){
                exit(json_encode(array("status"=>-1,"msg"=>$result[&#39;msg&#39;],&#39;result&#39;=>&#39;&#39;)));
            }

            $add_time = time();
            if(!empty($res_edit))
            {
                $where=array(&#39;user_id&#39; => $user_id,&#39;fangchan_id&#39; => $fangchan_id,&#39;cate_id&#39;=> $cate_id);
                $r_info = M(&#39;fangchan_jindiao&#39;)->where($where)
                    ->getField(&#39;jindiao_id&#39;);

                if($r_info)
                {
                    foreach ($result[&#39;result&#39;] as $v)
                    {
                        if($type_file==&#39;file&#39;)
                        {
                            $data=array(&#39;user_id&#39; => $user_id,&#39;fangchan_id&#39; => $fangchan_id,&#39;cate_id&#39;=> $cate_id,&#39;add_time&#39;=> $add_time,&#39;jindiao_id&#39;=> $r_info,&#39;file&#39;=>$v);
                            $ziyuan_info = M("fangchan_jdresources")->data($data)->add();
                        }elseif($type_file==&#39;image&#39;)
                        {
                            $data=array(&#39;user_id&#39; => $user_id,&#39;fangchan_id&#39; => $fangchan_id,&#39;cate_id&#39;=> $cate_id,&#39;add_time&#39;=> $add_time,&#39;jindiao_id&#39;=> $r_info,&#39;images&#39;=>$v);
                            $ziyuan_info = M("fangchan_jdresources")->data($data)->add();
                        }

                    }


                    if($ziyuan_info)
                    {
                        $ret =[
                            &#39;status&#39;=>200,
                            &#39;msg&#39;=>&#39;上传成功&#39;,
                            &#39;data&#39;=> $data
                        ];
                    }else{
                        $ret =[
                            &#39;status&#39;=>1009,
                            &#39;msg&#39;=>&#39;上传资源失败&#39;,
                            &#39;data&#39;=> &#39;&#39;
                        ];
                    }
                }else{
                    $ret =[
                        &#39;status&#39;=>1008,
                        &#39;msg&#39;=>&#39;上传资源失败&#39;,
                        &#39;data&#39;=> &#39;&#39;
                    ];
                }
            }elseif(!empty($res_add))
            {
                foreach ($result[&#39;result&#39;] as $v)
                {
                    if($type_file==&#39;file&#39;)
                    {
                        $data=array(&#39;user_id&#39; => $user_id,&#39;fangchan_id&#39; => $fangchan_id,&#39;cate_id&#39;=> $cate_id,&#39;add_time&#39;=> $add_time,&#39;jindiao_id&#39;=> $res_add,&#39;file&#39;=>$v);
                        $ziyuan_info = M("fangchan_jdresources")->data($data)->add();
                    }elseif($type_file==&#39;image&#39;)
                    {
                        $data=array(&#39;user_id&#39; => $user_id,&#39;fangchan_id&#39; => $fangchan_id,&#39;cate_id&#39;=> $cate_id,&#39;add_time&#39;=> $add_time,&#39;jindiao_id&#39;=> $res_add,&#39;images&#39;=>$v);
                        $ziyuan_info = M("fangchan_jdresources")->data($data)->add();
                    }

                }
                if($ziyuan_info)
                {
                    $ret =[
                        &#39;status&#39;=>200,
                        &#39;msg&#39;=>&#39;上传成功&#39;,
                        &#39;data&#39;=> $data
                    ];
                }else{
                    $ret =[
                        &#39;status&#39;=>1010,
                        &#39;msg&#39;=>&#39;上传资源失败&#39;,
                        &#39;data&#39;=> &#39;&#39;
                    ];
                }
            }else{
                $ret =[
                    &#39;status&#39;=>1011,
                    &#39;msg&#39;=>&#39;上传资源失败&#39;,
                    &#39;data&#39;=> &#39;&#39;
                ];
            }
        }elseif(empty($res_add) && empty($res_edit)){
            $ret =[
                &#39;status&#39;=>1007,
                &#39;msg&#39;=>&#39;上传失败&#39;,
                &#39;data&#39;=> &#39;&#39;
            ];
        }else{
            $ret =[
                &#39;status&#39;=>200,
                &#39;msg&#39;=>&#39;上传成功&#39;,
                &#39;data&#39;=> &#39;&#39;
            ];
        }
       $this->ajaxReturn($ret);
    }

    /*

    *多图上传

     */
    public function uploadFile($type=&#39;file&#39;){

        if($type==&#39;file&#39;)
        {
            $type_info = array(&#39;doc&#39;, &#39;docx&#39;, &#39;xls&#39;, &#39;xlsx&#39;,&#39;zip&#39;,&#39;rar&#39;);
            $type_path = &#39;/Public/upload/jidiao/files/&#39;;
        }elseif($type==&#39;image&#39;){
            $type_info = array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;);
            $type_path = &#39;/Public/upload/jidiao/images/&#39;;
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     1 * 1024 * 1024;// 设置附件上传大小
        $upload->exts      =     $type_info;// 设置附件上传类型
        $upload->rootPath  =      &#39;.&#39;.$type_path; // 设置附件上传根目录
        $upload->savePath  =      &#39;&#39;; // 设置附件上传(子)目录
        $upload->subName   = array(&#39;date&#39;,&#39;Y/m-d&#39;);
        //上传文件
        $info = $upload->upload();
        $picurl = array();
        if(!$info) {// 上传错误提示错误信息
            return array(&#39;status&#39;=>-1,&#39;msg&#39;=>$upload->getError(),&#39;result&#39;=>&#39;&#39;);
        }else{// 上传成功 获取上传文件信息
            foreach($info as $file){
                $picurl[] = $type_path.$file[&#39;savepath&#39;].$file[&#39;savename&#39;];
            }
            return array("status"=>1,"msg"=>&#39;上传成功&#39;,&#39;result&#39;=>$picurl);

        }



    }
Salin selepas log masuk

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

ThinkPHP5微信现金红包的开发

关于thinkphp行为的使用

Atas ialah kandungan terperinci thinkphp 下多图ajax上传图片. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan