Laravel 5,尝试多文件上传,Request :: file()只返回最后一个文件? [英] Laravel 5, attempting multi-file upload, Request::file() only returning last file?

查看:114
本文介绍了Laravel 5,尝试多文件上传,Request :: file()只返回最后一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel 5的 Request facade来使用相同的密钥上传多个文件。从我在其他地方读到的,正确的方法是调用 Request :: file()而不将参数传递给 :: file() method。

I'm attempting to get multiple files uploaded using the same key using Laravel 5's Request facade. From what I've read elsewhere, the correct way to do this is to call Request::file() without passing a parameter to the ::file() method.

但是,这似乎只返回请求中发送的最后一个文件。

However, this only seems to return the last file sent in the request.

标题

POST /test/service/upload HTTP/1.1
Host: www.****.dev
X-CSRF-TOKEN: 2DQBuTuy50EELFen5vXFaOv1cyXICmAISUx8LoCS
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="photo"; filename="10464005_10152969193248906_6272325120604924631_n.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="photo"; filename="10458555_10152969192978906_1569926627111581344_n.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="photo"; filename="10365774_10152969188498906_1884545544754633531_n.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C

PHP

    $files = Request::file();
    $names = [];

    foreach ($files as $file) {
        $names[] = $file->getClientOriginalName();
    }
    return $names;

回复

[
    "10365774_10152969188498906_1884545544754633531_n.jpg"
]

我是否需要为此工作正确设置任何类型的配置或标头?如果它有帮助,这将是一个基于AJAX的请求,我一直在使用谷歌浏览器扩展邮差来测试这个。

Is there any kind of configuration or headers that I have to set for this work appropriately? If it helps, this will be an AJAX based request and I've been using the Google Chrome extension "Postman" to test this.

任何帮助将不胜感激!

Any help would be greatly appreciated!

推荐答案

使用文件元素数组作为html,如下所示

use array of file element as html like follow

<input type="file" name="photo[]">
<input type="file" name="photo[]">

在表单和laravel中添加enctype属性以获取文件使用文件的密钥如下

add enctype attribute in form and in laravel to get file use the key of the file as follow

$files = Request::file('photo');
    $names = [];

    foreach ($files as $file) {
        $names[] = $file->getClientOriginalName();
    }
    return $names;

据我说它应该有效。

这篇关于Laravel 5,尝试多文件上传,Request :: file()只返回最后一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆