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



        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM