【问题标题】:Laravel Input::file returning empty array while $_FILES has dataLaravel Input::file 返回空数组,而 $_FILES 有数据
【发布时间】:2015-04-19 19:44:18
【问题描述】:

为了简单起见,我认为:

{{ Form::open(['route' => 'files', 'method' => 'POST', 'files' => 'true']) }}
{{ Form::file('file') }}
{{ Form::submit(); }}
{{ Form::close() }}

在我的控制器中:

return Response::json([$_FILES, print_r(Input::file('file'),1)]);

这是我提交时得到的回复:

[
    {
        "file": {
            "name": "sample.jpg",
            "type": "image/jpeg",
            "tmp_name": "/tmp/phpItZT7K",
            "error": 0,
            "size": 17645
        }
    },
    {}
]

我在搜索时遇到的唯一真正的解决方案是 enctype,我通过 Form::open 中的 'files' 属性在我的表单上拥有它。在这一点上,我不知道发生了什么。它不会破坏应用程序,但它仍然很烦人。如果有人能对此有所了解,我会非常高兴。

【问题讨论】:

  • 您面临的问题是什么...??
  • Input::file() 没有返回任何数据。没有 UploadedFile 对象或任何东西。从那以后,它在两次刷新之间神奇地重新开始工作,而我没有更改任何相关的代码,或者我相信。
  • 我们遇到了同样的问题。 $_FILES 显示了一个包含文件的数组,但 Input::file('file') 没有显示任何内容。

标签: php laravel file-upload laravel-4


【解决方案1】:

您还没有澄清实际问题是什么,但我猜这是 Input::file() 没有通过 JSON。那是因为Input::file() 返回一个无法编码的symfony object,所以您必须创建自己的数组。

$file = Input::file('file');
$output = ['name' => $file->getClientOriginalName(), 'size' => $file->getClientSize()]; // etc

【讨论】:

  • 我正在使用 print_r(Input::file('file'),1) 在 JSON 输出中打印,它始终为空白。我只是忘记在帖子中包含它。
  • 尝试使用 Log 类来记录对象,看看会发生什么。您实际上是在单击按钮还是在使用 AJAX?
猜你喜欢
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2018-07-30
相关资源
最近更新 更多