php Laravel $request->file() 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38226875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:31:40  来源:igfitidea点击:

Laravel $request->file() returns null

phplaravelfile-upload

提问by Renārs Vilnis

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

尝试在后端使用 Laravel 上传文件时遇到问题。

Issue

问题

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

Laravel$request->file()方法返回 null。

Setup

设置

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

我使用superagent构建了一个 AJAX 请求,调试了请求,一切看起来都很好。的Content-Length依赖于图像I附加的变化,指示图像已被添加到该请求。该Content-Type也被设置为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 $_FILESarray, I noticed that a my file has been uploaded.

但是我无法在 Laravel 中获取该文件。使用$request->file('files')return NULL,但如果我调试$_FILES数组,我注意到我的文件已上传。

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-Typeof the input file not being application/octet-stream?

什么可能导致 Laravel 忽略该文件?
Content-Type输入文件不是application/octet-stream

Below have answered the question.

下面已经回答了问题。

采纳答案by Renārs Vilnis

Noticed that the $requestobject 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.

请注意,$request我在 Controller 方法中接收的对象是 Instance of JsonRequest,它是一个扩展的自定义类(目前为空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 $requestsolves my issue

我得到所需的输入文件。现在切换实例$request解决了我的问题

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

但我不明白为什么/如何扩展Illuminate\Http\Request空类会破坏事情。
有人可以解释一下吗?

My intention with subclassing Illuminate\Http\Requestwas to be able to attach methods on $requestsfor 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.

我对子类化的意图Illuminate\Http\Request是能够附加方法$requests,以统一的方式处理 API 请求的异常/错误。例如在部署时显示异常消息,但在生产中返回固定消息。
有没有不同/更好/更多的 Laravel 方法来做到这一点?
我认为只会创建一个 JsonController 而不是扩展请求。

回答by CarlosYanes

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

您应该添加到表单标签 '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!

我希望这能帮到你!

回答by Jeremy Quick

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

我只在服务器上有这个问题。图像存储在$_FILES指向php.ini文件中物理目录的变量中。确保您对此目录具有正确的权限。

php.ini:

php.ini:

upload_tmp_dir = /Applications/MAMP/tmp/php

upload_tmp_dir = /Applications/MAMP/tmp/php