having trouble trying to upload files using Laravel on the back-end.
Laravel $request->file() method returns null.
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.
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!
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\Requestwith 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With