laravel 如何使用输入文件的数组名称请求文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30770347/
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-09-14 11:45:45  来源:igfitidea点击:

How to request file with array name of input file?

phphtmllaravel

提问by Tum Lina

Just wondering why I can't get value of array name from input type file.

只是想知道为什么我无法从输入类型文件中获取数组名称的值。

HTML:

HTML:

<input type="file" name="collateral_photo[]" id="collateral_photo" class="default"/>

The name of this input file is an array.

此输入文件的名称是一个数组。

PHP Laravel

PHP Laravel

return Request::file('collateral_photo');

The return result is [{}]

返回结果为 [{}]

What I want right now just how to get value from this input file with array name.

我现在想要的是如何从这个带有数组名称的输入文件中获取值。

Please, please help me out.

拜托,请帮帮我。

Thank you very much for any help.

非常感谢您的帮助。

回答by Sourabh Kumar Sharma

use the below code, since the name attribute is an array

使用下面的代码,因为 name 属性是一个数组

$files = Request::file('collateral_photo');
return $files[0];

or if you want second file

或者如果你想要第二个文件

return $files[1];

//if you want to access second file and so on

you need to access the file with array index specified.

您需要访问指定了数组索引的文件。

If you want to return the whole files array itself then use

如果你想返回整个文件数组本身然后使用

return $files

回答by Magno Max

If it is an array of files do:

如果它是一个文件数组,请执行以下操作:

$request()->file($fieldName)[$key]->getClientOriginalName();

回答by Yevgeniy Afanasyev

And going one step further we can work with multiple-level-arrays like so:

更进一步,我们可以使用多级数组,如下所示:

<input name="channel[1][file]" type="file">
<input name="channel[2][file]" type="file">

in laravel you would need a structure

在 Laravel 中,您需要一个结构

$files = $request->file('channel');
$file1 = $files[1]['file'];
$file2 = $files[2]['file'];