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()); } } }