【问题标题】:Error "Call to a member function getClientOriginalExtension() on null" in laravel 5.3?laravel 5.3 中的错误“在 null 上调用成员函数 getClientOriginalExtension()”?
【发布时间】:2017-02-02 15:11:05
【问题描述】:

我正在对图像上传功能使用干预,但是当我尝试从邮递员上传带有标题中“内容类型:多部分/表单数据”的图像时,它会显示上述错误,但如果我删除内容类型标题然后它工作正常。我添加了邮递员的图像。

我的路线

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;

  }

【问题讨论】:

  • 您的照片尚未上传。尝试用这张 $photo[0] 代替照片
  • 我应该在哪里改变这个?
  • 尝试打印 $photo 变量并检查值。还要检查您的表单,它应该是 enctype="multipart/form-data"。
  • @shafiq 我使用 laravel 作为 API 那么如何检查这个 enctype 呢?
  • 我尝试使用 dd($photo) 打印 $photo,所以我得到了空数组作为响应。

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


【解决方案1】:

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

还有在你HTML表单中添加这个

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

或者如果你使用 laravel 集体,那么在表单中添加 'files'=&gt; 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;

}

【讨论】:

  • 好。但是请求我的 api 并尝试上传文件的 android 和 ios 设备呢?他们如何更改请求格式?
  • 因为他们总是将 RequestHeader 设置为 content-type:multipart/form-data。
  • 其实问题不在于。我在邮递员中添加了内容类型标头,因此它无法正常工作,但是如果我删除此内容类型标头,则它可以正常工作。但我假设像 android 和 ios 这样的设备正在发送带有 content-type: multipart/form-data 的请求,所以我应该在 postman 中添加它。所以这就是我感到困惑的原因。
猜你喜欢
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多