计算机网络/计算机科学与应用/系统/运维/开发

ThinkPHP5.1 上传图片注意事项

错误一:thinkphp5.1 致命错误: 

Call to a member function validate() on null

注意表单添加:enctype="multipart/form-data"


注意二:上传图片要进行try,不然要报警告没有上传文件file

try{
    $file = request()->file('img');
    $info = $file->validate(['ext'=>'jpg,png,gif'])->move( '../public/uploads/');
    if($info){
       $data['img'] = str_replace("\\","/",$info->getSaveName());
    }else{
       $res = $file->getError();
       return $this->sendError($res);
    }
 }catch (\Exception $e) {
 }


tp5.1 图片上传

顺,不妄喜;逆,不惶馁;安,不奢逸;危,不惊惧。--史记

评论

^