错误"在null"上调用成员函数getClientOriginalExtension();在laravel 5.3?

时间:2017-02-02 06:34:46

标签: laravel laravel-5 laravel-5.3 postman intervention

我正在使用干预功能进行图片上传功能,但是当我尝试使用" content-type:multipart / form-data"从邮递员上传图片时在标题中,它显示上面的错误,但如果我删除内容类型标题,那么它工作正常。我添加了邮递员的图像。 Postman header with request **Postman image upload**

我的路线

  

路线::交('接触/图像/上传' ['如' =>' intervention.postresizeimage''使用&# 39; =>'的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;

  }

1 个答案:

答案 0 :(得分:1)

  

输入中缺少文件,因此它变为空,因此建议使用   检查输入请求是否有文件并继续返回   错误。

同样在你HTML表单中添加此

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

或者如果您使用的是laravel collective,请在表单

中添加'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;

}