laravel 5.3 中的错误 "Call to a member function getClientOriginalExtension() on null"?

Error "Call to a member function getClientOriginalExtension() on null" in laravel 5.3?

我正在对图片上传功能使用干预,但是当我尝试使用 "content-type: multipart/form-data" 在 header 中从邮递员上传图片时,它会显示上述错误,但是如果我删除 content-type header 那么它工作正常。我添加了邮递员的图像。

我的路线

Route::post('contact/image/upload',['as'=>'intervention.postresizeimage','uses'=>'contactController@upload_image']);

控制器中的代码

public function upload_image(Request $request){

      if((preg_match("/^[789]\d{9}$/", $request->header('UID')))){

        $photo = $request->file('image');
        $imagename = time().'.'.$photo->getClientOriginalExtension(); 

        $destinationPath_thumb = storage_path('images/thumbnail_images');
        $thumb_img = Image::make($photo->getRealPath())->resize(100, 100);
        $thumb_img->save($destinationPath_thumb.'/'.$imagename,80);

        $destinationPath_medium = storage_path('images/medium_images');
        $medium_img = Image::make($photo->getRealPath())->resize(500, 500);
        $medium_img->save($destinationPath_medium.'/'.$imagename,80);

        $destinationPath_original = storage_path('images/original_images');
        $photo->move($destinationPath_original, $imagename);

        $user = \App\User::select(['inst_id'])->where('mobile','=',$request->header('UID'))->first();

        $update_img = \App\Contact::where([['id','=',$request->ID],['inst_id','=',$user->inst_id]])->update(['image'=>$imagename]);

        if($update_img)
          $response = response()->json(['data'=>[], 'error'=>0,  'error_msg'=>'', 'message'=>'Profile updated']);
        else
          $response = response()->json(['data'=>[], 'error'=>1,  'error_msg'=>'some went wrong', 'message'=>'Please try again']);
      }
      else
         $response = response()->json(['data'=>[], 'error'=>1,  'error_msg'=>'wrong mobile in UID header','message'=>'wrong mobile no. in header']);

    return  $response;

  }

File is missing in your input so it is becoming null, so it is advised to check if there is file on input request and continue else return error.

并且也在你的 HTML 表单中添加这个

<form enctype="multipart/form-data">

或者,如果您使用的是 laravel 集体,则在表格

中添加 'files'=> true

并且我已经更新了您的代码以检查文件是否存在于下方

public function upload_image(Request $request){

  if((preg_match("/^[789]\d{9}$/", $request->header('UID')))){

    if(!$request->hasFile('image')) {

      return response()->json(['data'=>[], 'error'=>1,  'error_msg'=>'image file is required', 'message'=>'image file is required for upload']);
    }

    $photo = $request->file('image');
    $imagename = time().'.'.$photo->getClientOriginalExtension(); 

    $destinationPath_thumb = storage_path('images/thumbnail_images');
    $thumb_img = Image::make($photo->getRealPath())->resize(100, 100);
    $thumb_img->save($destinationPath_thumb.'/'.$imagename,80);

    $destinationPath_medium = storage_path('images/medium_images');
    $medium_img = Image::make($photo->getRealPath())->resize(500, 500);
    $medium_img->save($destinationPath_medium.'/'.$imagename,80);

    $destinationPath_original = storage_path('images/original_images');
    $photo->move($destinationPath_original, $imagename);

    $user = \App\User::select(['inst_id'])->where('mobile','=',$request->header('UID'))->first();

    $update_img = \App\Contact::where([['id','=',$request->ID],['inst_id','=',$user->inst_id]])->update(['image'=>$imagename]);

    if($update_img)
      $response = response()->json(['data'=>[], 'error'=>0,  'error_msg'=>'', 'message'=>'Profile updated']);
    else
      $response = response()->json(['data'=>[], 'error'=>1,  'error_msg'=>'some went wrong', 'message'=>'Please try again']);
  }
  else
     $response = response()->json(['data'=>[], 'error'=>1,  'error_msg'=>'wrong mobile in UID header','message'=>'wrong mobile no. in header']);

return  $response;

}