如何循环遍历 $_FILES 数组?
这是我想要循环的输入
Main photo: <input type="file" name="image[]" />
Side photo 1: <input type="file" name="image[]" />
Side photo 2: <input type="file" name="image[]" />
Side photo 3: <input type="file" name="image[]" />
发生了一些奇怪的事情,当我上传任何内容时我使用 count($_FILES['image'])
,我回显了该函数,并且它返回一个值共 5 个。该数组中不应有任何元素。当我一开始只有 4 个文件时,为什么还要有一个额外的输入?
现在,随着实际的循环本身,我尝试使用 foreach 循环,但它不起作用。
foreach($_FILES['image'] as $files){echo $files['name']; }
什么也没发生,我最终想做的是循环所有图像,确保它们的格式、大小正确,并重命名每个图像。但这个简单的 foreach() 循环表明,不知何故,我什至无法循环遍历 $_FILES 数组,而 count() 当我什至没有上传任何内容时说数组中有 5 个元素时,让我更加困惑。
Here are the inputs I want to loop through
Main photo: <input type="file" name="image[]" />
Side photo 1: <input type="file" name="image[]" />
Side photo 2: <input type="file" name="image[]" />
Side photo 3: <input type="file" name="image[]" />
A couple weird things happened, when I uploaded nothing I use the count($_FILES['image'])
, I echoed that function, and it returns a value of 5. There should be no elements in that array. Why is there one extra input when I only have 4 files to begin with?
Now with the actually looping itself, I try using the foreach loop, but it doesn't work.
foreach($_FILES['image'] as $files){echo $files['name']; }
Nothing came up, what I wanted to ultimately do is to loop through all images, make sure they are correct format, size, and rename each of them. But this simple foreach() loop shows that somehow I can't even loop through the $_FILES array and the count() confused me even more when it say that there are 5 elements in the array when I didn't even upload anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您的示例表单应该可以正常工作。只是当使用数组结构作为字段名称时,您期望
$_FILES
超全局的结构与实际不同。这个多维数组的结构如下:
因此
count( $_FILES[ "fieldname" ] )
将产生5
。但计算更深的维度也不会产生您可能期望的结果。例如,使用
count( $_FILES[ "fieldname" ][ "tmp_name" ] )
对字段进行计数,结果将始终是文件字段的数量,而不是实际上传的文件的数量。您仍然需要循环遍历元素来确定是否已为特定文件字段上传任何内容。编辑
因此,要循环遍历字段,您可以执行如下操作:
有关详细信息,请参阅
Your example form should work fine. It's just that you are expecting the structure of the
$_FILES
superglobal to be different than it actually is, when using an array structure for the field names.The structure of this multidimensional array is as followed then:
Therefor
count( $_FILES[ "fieldname" ] )
will yield5
.But counting deeper dimensions will also not produce the result you may expect. Counting the fields with
count( $_FILES[ "fieldname" ][ "tmp_name" ] )
for instance, will always result in the number of file fields, not in the number of files that have actually been uploaded. You'd still have to loop through the elements to determine whether anything has been uploaded for a particular file field.EDIT
So, to loop through the fields you would do something like the following:
For more information see the docs.
将 $_FILES['files'] 重建为更预期的结构的短函数。
Short function to rebuild $_FILES['files'] to some more expected structure.
只需以这种方式重命名您的字段
,然后您就可以以通常的方式迭代它:
just rename your fields this way
and then you'll be able to iterate it usual way:
我想出了一个适用于任意深度的 $_FILES 数组的解决方案。作为快速解释,您需要一个执行此操作的算法:
这是一些实际有效的代码。
只需对 $_FILES 调用 sane_files_array 就可以了,无论 $_FILES 数组有多深。这确实应该是语言本身的一部分,因为 $_FILES 数组的格式绝对是荒谬的。
I came up with a solution that works for $_FILES arrays of arbitrary depth. As a quick explanation, what you need an algorithm that does this:
Here's some code that actually works.
Just call sane_files_array on $_FILES and you should be good to go, regardless of how deep the $_FILES array is. This really should be part of the language itself, because the formatting of the $_FILES array is absolutely ridiculous.
也许:
如果您必须循环遍历特定的文件字段。
Maybe:
If you have to loop through specific file fields.
PHP 选择如何处理 $_FILES 浪费了开发人员的大量时间。基于@Lendrick 的回答,这里有一个类似的面向对象方法。
PHP's choice of how to handle $_FILES wastes a lot of developer time. Based on @Lendrick's answer, here is a similar OO approach.
我喜欢@Lendrick 重建数组的尝试。我完全同意 PHP 中原始的 $_FILES 数组纯粹是疯狂的。
替代方案 1
我想出了这个支持多维数组的函数,例如
的输出示例:
使用示例:
替代 2
另一种变体,它提供了更扁平的效果可以方便的数组是这样的:
它返回以下
:
使用示例:
I like the attempt by @Lendrick to reconstruct the array. I totally agree that the original $_FILES array is purely insane in PHP.
Alternative 1
I came up with this function that supports multidimensional arrays such as
<input type="file" name="a[b][c]" />
Example output for
<input type="file" name="image[]" multiple />
:Example of use:
Alternative 2
Another variant which gives a more flattened array that can be handy is this:
Which returns the following for
<input type="file" name="foo[bar][]" multiple />
:Example of use:
我对这个答案已经很晚了,但我厌倦了一遍又一遍地解决处理 PHP 文件数组的问题,所以我编写了一个 Composer 包,这样我就再也不用这样做了。也许有人在谷歌上搜索会找到我的答案并感到高兴。
安装
tvanc /files-array-organizer
将
$_FILES
传递给它,它会返回一个按照您期望的方式构造的数组。I'm way late to the game for this answer but I got tired of solving the problem of dealing with the PHP files array over and over, so I wrote a composer package so I'd never have to again. Maybe someone googling will find my answer and be happy.
Install
tvanc/files-array-organizer
Pass
$_FILES
to it, and it'll give you back an array that's structured the way you were expecting.我已经在这个困境中挣扎了快一周了!我在网上找到的任何东西都无法帮助我。我知道该怎么做,但无法弄清楚如何正确循环 $_FILES 数组 - 直到现在我阅读了已接受答案的编辑后的帖子。
不过,我在发布的脚本中做了一些更改,因为它对我来说不能正常工作。我希望能够确定是否选择了文件,所以我更改了该行
"if( !empty( $_FILES[ 'image' ][ 'error' ][ $index ] ) )"
到
"if( !empty( $_FILES[ 'image' ][ 'size' ][ $index ] ) )"
然后我将大小放入变量中,而不是“return false;”:
“$Size = $_FILES['上传']['大小'][$index];”
这样我可以检查 $Size 变量是否大于零。如果是,则已选择一个文件,我可以继续计算文件数量并进行实际上传。在接受的答案中,我没有在“return false;”之后使用任何“不必要的”脚本。希望这对某人有帮助。
:P
/MacD
I have struggled with this dilemma for almost a week! Nothing I found on the net could help me. I knew sort of what to do, but could not figure out how to loop through the $_FILES array properly - until now when I read the edited post of the accepted answer.
I made some changes though, in the script as posted, as it did not work properly for me. I wanted to be able to determine if a file was selected at all, so I changed the line
"if( !empty( $_FILES[ 'image' ][ 'error' ][ $index ] ) )"
to
"if( !empty( $_FILES[ 'image' ][ 'size' ][ $index ] ) )"
and then instead of "return false;", I put the size into a variable instead:
"$Size = $_FILES[ 'upload' ][ 'size' ][ $index ];"
This way I could check if the $Size variable was larger than zero. If it was, then a file had been selected and I could continue with counting the number of files and do the actual upload. I did not use any of the "unnecessary" script after "return false;", in the accepted answer. Hope this helps someone.
: P
/MacD
如果你不能加入他们,那就打败他们。
If you can't join them beat them.
https://gist.github.com/noorwachid/fce70a3a2d96502c2805248c46fc23f9
将临时路径与文件名组合起来,将产生一个数组,如下所示:
代码:
Combining temporary path with the filename which will result an array like:
The code: