Dan Marks
Dan Marks

Reputation: 49

Uploading images with laravel 5.2

I am trying to create a simple upload images function for my laravel project however I keep getting this error:

FatalThrowableError in UploadController.php line 23:
Fatal error: Call to a member function hasFile() on null

This is my controller:

  public function uploadImg(Request $request){
    $input = $request->input();
    if($input->hasFile('file')){
      echo 'Uploading';
      $file = $input->file('file');
      $file->move('uploads', $file->getClientOriginalName());
      echo 'Uploaded';
    }
  }

This is my form:

<form action="/admin/media/uploadImg" method="post" enctype="multipart/form-data">
    <label>Select image to upload:</label>
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload" name="submit">
    <input type="hidden" value="{{ csrf_token() }}" name="_token">
  </form>

Upvotes: 1

Views: 760

Answers (1)

Samsquanch
Samsquanch

Reputation: 9146

The hasFile() method only works on the request object, not the input array. Try this instead:

if($request->hasFile('file')){

See: https://laravel.com/docs/5.2/requests#files

You'll also need to change this line:

$file = $input->file('file');

To:

$file = $request->file('file');

Upvotes: 1

Related Questions