Laravel $request->file() returns null
Asked Answered
C

6

54

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.

Calcicole answered 6/7, 2016 at 14:42 Comment(0)
V
119

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!

Valdes answered 8/3, 2018 at 11:46 Comment(2)
I was looking at this answer and I thought I have it but I had two files - one for create and another for edit screen. So after couple hours I saw this answer again and realized that I should double check and that was exactly the problem. I am dumb XDOverpass
even after years of experience, I need someone to remind me to add this :DDuprey
C
7

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.

Calcicole answered 6/7, 2016 at 14:42 Comment(3)
try adding a constructor so you can link JsonRequest with RequestAdminicle
did you injected Request in the method ?Limit
@AniruddhaChakraborty how I go about doing that? Well I'm starting to figure out that the app/Http/Requests are used mainly as FormRequests to remove validation from controllers. But with JsonRequest I wanted the $request instance to have additonal messages with handling exceptions and returning error messages.Insufferable
P
3

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

Pourboire answered 3/1, 2020 at 21:47 Comment(0)
G
1

I had this problem too, had added the enctype="multipart/form-data" part in the form, tried all the solutions posted here...

But In my case the problem was in the javascript. I was creating the <input type"file" /> dinamically from another input, and was copying the props file object from one another, posting it to laravel and then in laravel $request->input was null:

$('#take-picture').on('change', function (){
 let props = $(this).prop('files');
 ...
 let $input = $('<input type="file" class="d-none" />');
 $input.prop('name', 'image_one').prop('files', props);
 $(this).val(''); //This is the problem in my case
 ...
 $form.append($input);
 $form.submit();

The problem was in this line:

$(this).val('');

Because I was obtaining the props object and assigning it to the created input, and the setting it to null to delete the selected value in the original input. but this object is obtained by reference, so when I was seleting it from the original input, I was deleting it from the newly created too, and so when posting the form, laravel got null.

So the solution was simply to obtain a clone of the props object and use it (I used the solution to clone a FileList posted in this answer: Is it possible to update FileList?

let props = $(this).prop('files')
...
let cloneProps = new FileListItems(props);
$input.prop('name', 'image_' + image_index).prop('files', cloneProps)

Hope it helps somebody!

Gustave answered 6/6, 2023 at 9:26 Comment(0)
H
1

Check

$request->file('your_param_name')->getErrorMessage()

this will give you a meesage to know whats wrong with the temp upload. And try testing with files which are smaller in size.

Check in php.ini

upload_max_filesize 

If the file size is above this limit it will not upload and not even throw an exception.

Honk answered 3/6, 2024 at 12:50 Comment(0)
C
0

Check your php.ini settings for:

  • file_uploads
  • upload_max_filesize
  • max_file_uploads

I had mine turned off. I guess that makes it quietly wipe out the file.

Or run php -i | grep --color=always upload to check your current settings.

Coffee answered 19/9, 2021 at 6:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.