当$ _FILES有数据时,Laravel Input :: file返回空数组

时间:2015-02-18 17:23:46

标签: php laravel file-upload laravel-4

为了简单起见,我认为这是:

{{ 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中的属性。在这一点上,我不知道发生了什么。它并没有破坏应用程序,但它仍然很烦人。如果有人能够对此有所了解,我会非常高兴。

1 个答案:

答案 0 :(得分:2)

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

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