thinkphp5文件上传问题


tp5中文件上传如果没有数据就会报错,所以要先做一个判断


//先接收文件数据


$isfile=$_FILES;
//判断是否上传图片数据,如果没有上传数据二位数组中的name会为空,如下例:
if($isfile['brand_logo']['name']==''){


}else{


}


下面是一个完整的图片上传代码



if(request()->isPost()){
            $brand=model('brand');
            $data=$_POST;
            $isfile=$_FILES;
            //判断是否上传图片
            if($isfile['brand_logo']['name']==''){
                $res=$brand->add($data);
                if($res['valid']){
                    $this->success($res['msg'],'lst');

                }else{
                    $this->error($res['msg']);
                }
            }else{
                $file = request()->file('brand_logo');
                $info = $file->validate(['size'=>155678,'ext'=>'jpg,png'])->move( '.\static\uploads\brand_logo');

                if($info){

                    $data['brand_logo']='.\static\uploads\brand_logo\\'.$info->getSavename();
                    $res=$brand->add($data);
                    if($res['valid']){
                        $this->success($res['msg'],'lst');

                    }else{
                        $this->error($res['msg']);
                    }
                }else{
                    //输出验证错误提示和图片移动错误提示
                    $this->error($file->getError());
                }
            }

        }