Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: File upload not working

I'm trying to upload an image via an HTML form in Laravel 5.5. I have included the enctype="multipart/form-data" attribute, still nothing happens.

Form code:

<form method="POST" enctype="multipart/form-data">
  {{ csrf_field() }}
  <div class="form-group">
    <label for="m_photo" class="col-md-4 control-label">Main photo</label>
    <div class="col-md-6">
      <input id="m_photo" type="file" class="form-control-file space" name="m_photo" required>
    </div>
  </div>

  <div class="form-group">
    <div class="col-md-6 col-md-offset-4">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

Route (web.php) code:

Route::post('smartphones/entry', 'HomeController@s_submit')->name('s_submit');

Controller code:

public function s_submit() {
  if (Input::hasFile('m_photo')) {
      // doing something
  }

  else {
      echo 'Nothing happened';
  }
}

'Nothing happened' is echoed out when I submit the form.

It's interesting that when I do this:

public function s_submit(Request $request) {
    $input = Input::all();
    dd($input);
}

I see:

array:1 [
"m_photo" => UploadedFile {#210 ▶}
]

It's like the image is getting passed, but I'm unable to retrieve it. Please help.

like image 219
Raj Mehta Avatar asked Mar 08 '23 01:03

Raj Mehta


1 Answers

This can happen when PHP max_file_size is not set to a size that allows the file you are trying to upload to be sent. This causes hasFile returns false, when, for example, file->getClientOriginalName() works.

Try to check upload_max_filesize or post_max_size in your php.ini, or try with a smaller file to check if it works.

like image 196
Laerte Avatar answered Mar 16 '23 09:03

Laerte