Laravel 5,尝试多文件上传,Request::file() 只返回最后一个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30021086/
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
Laravel 5, attempting multi-file upload, Request::file() only returning last file?
提问by AliBaba
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.
我正在尝试使用 Laravel 5 的Request
外观使用相同的密钥上传多个文件。从我在别处读到的,正确的方法是调用Request::file()
而不将参数传递给::file()
方法。
However, this only seems to return the last file sent in the request.
但是,这似乎只返回请求中发送的最后一个文件。
Headers
标题
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
PHP
$files = Request::file();
$names = [];
foreach ($files as $file) {
$names[] = $file->getClientOriginalName();
}
return $names;
Response
回复
[
"10365774_10152969188498906_1884545544754633531_n.jpg"
]
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.
我必须为此工作适当设置任何类型的配置或标头吗?如果有帮助,这将是一个基于 AJAX 的请求,我一直在使用 Google Chrome 扩展“邮递员”来测试它。
Any help would be greatly appreciated!
任何帮助将不胜感激!
回答by Sagar Rabadiya
use array of file element as html like follow
使用文件元素数组作为 html,如下所示
<input type="file" name="photo[]">
<input type="file" name="photo[]">
add enctype attribute in form and in laravel to get file use the key of the file as follow
在表单和 Laravel 中添加 enctype 属性以获取文件使用文件的密钥如下
$files = Request::file('photo');
$names = [];
foreach ($files as $file) {
$names[] = $file->getClientOriginalName();
}
return $names;
according to me it should work.
据我说它应该工作。