0

I am using intervention for image upload functionality but when I am trying to upload image from postman with "content-type: multipart/form-data" in header, it shows up above error, but if I remove content-type header then it is working fine. I have added images of postman . Postman header with request **Postman image upload**

my route

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

code in controller

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;

  }
SaMeEr
  • 361
  • 2
  • 7
  • 22
  • Your photo has not uploaded. try using this $photo[0] in place of photo – Vikash Feb 02 '17 at 06:44
  • Where should I change this ? – SaMeEr Feb 02 '17 at 06:47
  • Try printing $photo variable and check the value. Also check your form it should be enctype="multipart/form-data". – shafiq.rst Feb 02 '17 at 06:51
  • @shafiq I am using laravel as a API so how do i check this enctype ? – SaMeEr Feb 02 '17 at 06:53
  • I have tried to print $photo using dd($photo) so I got empty array in response. – SaMeEr Feb 02 '17 at 06:54
  • @SaMeEr $photo should give you Illuminate\Http\UploadedFile Object. Its means file is not uploading. Check your content type. http://stackoverflow.com/questions/16015548/tool-for-sending-multipart-form-data-request#answer-41435972 – shafiq.rst Feb 02 '17 at 07:04
  • @SaMeEr I think you are including content-type in your request. http://stackoverflow.com/questions/16015548/tool-for-sending-multipart-form-data-request#answer-41435972 – shafiq.rst Feb 02 '17 at 07:10
  • @shafiq . yes I am. but I think it should be added there because android and ios devices are requesting with this header which is not working. – SaMeEr Feb 02 '17 at 07:16
  • @SaMeEr Android and ios should send request with content-type:multipart/form-data however in postman you don't add it as postman already including it. For android and iso it will work only when content-type:multipart/form-data is set – shafiq.rst Feb 02 '17 at 09:43

1 Answers1

1

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.

And also in you HTML form add this

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

or if you are using laravel collective then add 'files'=> true in Form

and I have updated your code to do the checking if file is present at below

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;

}

msonowal
  • 1,553
  • 3
  • 17
  • 36
  • Good. but what about android and ios devices which is requesting to my api and trying to upload the file . how do they change the request format? – SaMeEr Feb 02 '17 at 07:14
  • Because they always set RequestHeader with content-type:multipart/form-data. – SaMeEr Feb 02 '17 at 07:23
  • Actually the problem is not that. I am adding content-type header in postman so it is not working but if I remove this content-type header then it is working fine. But I am assuming that devices like android and ios are sending request with content-type: multipart/form-data so I should add this in postman . so that's why I am confused. – SaMeEr Feb 02 '17 at 07:57