当前位置:Gxlcms > PHP教程 > thinkphp下多图ajax上传图片

thinkphp下多图ajax上传图片

时间:2021-07-01 10:21:17 帮助过:24人阅读

这篇文章主要介绍了关于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>

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

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

参数说明

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] : 'many_one'; //设置关系
        var formData = new FormData();        
        var fangchan_id = $('#fangchan_id').val();
        formData.append("fangchan_id", fangchan_id);
        formData.append("cate_id", cate_id);
        formData.append("guanxi", guanxi);
        formData.append("content", content);        
        if(type_file !='nofile'){
            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('file[]',$("#"+type_name)[0].files[i]);
            }
        }

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

            }

        });

    }

php代码比较长

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

        }

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

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


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

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

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

                    }


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

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

    /*

    *多图上传

     */
    public function uploadFile($type='file'){

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

        }



    }

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

相关推荐:

ThinkPHP5微信现金红包的开发

关于thinkphp行为的使用

以上就是thinkphp 下多图ajax上传图片的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行