Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel $request->file() returns null

having trouble trying to upload files using Laravel on the back-end.

Issue

Laravel $request->file() method returns null.

Setup

I build up an AJAX request using superagent, debugged the request and everything seems fine. The Content-Length changes depending on the image I add, indicating an image has been added to the request. The Content-Type is also set to multipart/form-data.

// request headers
Content-Length:978599
Content-Type:multipart/form-data; 

// request payload
Content-Disposition: form-data; name="files"; filename="item-keymoment.png"
Content-Type: image/png

But I'm unable to get the file in Laravel. Using $request->file('files') returns NULL, but if I debug the $_FILES array, I noticed that a my file has been uploaded.

dd($request->file('files'))
// NULL

dd($_FILES);
// array:1 [
//   "files" => array:5 [
//     "name" => "item-keymoment.png"
//     "type" => "image/png"
//     "tmp_name" => "/tmp/phpipbeeM"
//     "error" => 0
//     "size" => 978274
//   ]
// ]

dd($request->files->all())
// []

What might be causing Laravel to ignore the file?
Content-Type of the input file not being application/octet-stream?

Below have answered the question.

like image 568
Renārs Vilnis Avatar asked Jul 06 '16 14:07

Renārs Vilnis


3 Answers

You should add to the form tag enctype="multipart/form-data"

For example:

<form method="POST" action="{{route('back.post.new')}}" enctype="multipart/form-data">
.............
</form>

Adding it you can use your custom Request.

I hope this can you help!

like image 165
CarlosYanes Avatar answered Nov 14 '22 09:11

CarlosYanes


Noticed that the $request object I was receiving in the Controller method was an Instance of JsonRequest, which is a custom class (empty for now) that extends Illuminate\Http\Request.

And it's implemented as:

<?php

namespace App\Http\Requests;

use Illuminate\Http\Request;

class JsonRequest extends Request {
}

But if I change:

// from
use App\Http\Requests\JsonRequest;
public function add_background_image (JsonRequest $request) {
  dd($request->file('files'))
  // NULL
}

// to
use Illuminate\Http\Request;
public function add_background_image (Request $request) {
  dd($request->file('files'))
  // UploadedFile {#266
  //   -test: false
  //   -originalName: "item-keymoment.png"
  //   -mimeType: "image/png"
  //   -size: 978274
  //   -error: 0
  //   ...
  // }
}

I get the desired input file. For now switching the instance of $request solves my issue

But I don't understand why/how extending Illuminate\Http\Request with an empty Class breaks things.
Can someone explain?

My intention with subclassing Illuminate\Http\Request was to be able to attach methods on $requests for dealing with exceptions/errors in a unified way for API requests. Such as when in deployment show exception messages, but in production return a fixed message.
Is there different/better/more Laravel way of doing that?
I think will just create a JsonController instead of extending Request.

like image 42
Renārs Vilnis Avatar answered Nov 14 '22 09:11

Renārs Vilnis


I had this problem ONLY on the server. The image is stored in the $_FILES variable which points to a physical directory in your php.ini file. Make sure you have the correct permissions for this directory.

php.ini:

upload_tmp_dir = /Applications/MAMP/tmp/php

like image 3
Jeremy Avatar answered Nov 14 '22 10:11

Jeremy