Null value returned by Laravel’s $request->file() method

For Solution 2, use the POST method to send the file. It’s important to define the enctype parameter to inform the web server that you’re sending a file. Also, make sure you’ve defined in the router file that the POST method is used for the /addnews route, which refers to the addData controller action.
As for Solution 3, make sure you have the option to upload a file in your form definition. This option is mandatory to render the form with the enctype option in the HTML form tag, which allows file uploads.
Finally, for Solution 4, it’s important to note that the error is not in your backend code.

Question:

Encountering difficulties in implementing

upload files
with Laravel on the server-side.

Issue

The

$request->file()


function in Laravel results in a null value.

Setup

After constructing an AJAX request using superagent, I thoroughly checked and tested it. The value of

Content-Length


varies based on the image added, which confirms that the image has been included in the request. Additionally,

Content-Type


is always assigned

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

Despite uploading a file, I am facing issues in retrieving it through Laravel. The utilization of

$request->file('files')


results in

NULL


output. However, upon further inspection of the

$_FILES


array during debugging, I discovered that the uploaded file was present.

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 could be the reason for Laravel neglecting the file if the input file is not found?

I have provided a response to the query.


Solution 1:

Incorporate

enctype="multipart/form-data"


to the form tag.

For example:

.............

By incorporating it, you will be able to utilize your personalized inquiry.

I hope this can you help!


Solution 2:


In the Controller method, I observed that the object I obtained was an instance of a custom class (

JsonRequest


) that extends

IlluminateHttpRequest


. Currently, this custom class is empty.

And it’s implemented as:

But if I change:

// from
use AppHttpRequestsJsonRequest;
public function add_background_image (JsonRequest $request) {
  dd($request->file('files'))
  // NULL
}
// to
use IlluminateHttpRequest;
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
  //   ...
  // }
}

Obtaining the required input file was successful and my problem was resolved by simply changing the instance of

$request


at this time.

Could someone clarify why and how adding an empty Class to

IlluminateHttpRequest


leads to issues? I’m having difficulty comprehending.

To achieve a unified approach for handling exceptions/errors in API requests, I aimed to attach methods on

$requests


by subclassing

IlluminateHttpRequest


. For instance, displaying exception messages during deployment and returning a fixed message during production. Is there an alternative or improved method in Laravel for this purpose? I am considering creating a JsonController instead of

extending Request
.


Solution 3:


The issue occurred exclusively on the server. The image is saved in the

$_FILES


variable, which directs to a tangible path in your

php.ini


document. Verify that you possess the appropriate authorizations for this folder.

php.ini:

upload_tmp_dir = /Applications/MAMP/tmp/php


Solution 4:

Ensure that your

php.ini


configurations are appropriately set for:

  • file_uploads

  • upload_max_filesize

  • max_file_uploads

Since I turned off mine, the file was silently erased.

Execute

php -i | grep --color=always upload


to verify your

current settings
.

Frequently Asked Questions

Posted in Php