如何迭代PHP $ _FILES数组? [英] How can I iterate PHP $_FILES array?
问题描述
这可能是一个愚蠢的问题,但是PHP数组$ _FILES具有非常奇怪的格式,这是我以前从未使用过的格式.有人可以告诉我如何以理智的方式迭代此数组吗?我曾经迭代对象或对象,例如数组,但是这种格式对我来说很奇怪.有什么方法可以像对象数组那样迭代该数组吗?
This might be stupid question, but PHPs array $_FILES has very odd format, which I have never used before. Can somebody tell me how can I iterate this array in sane way ? I have used to iterate objects or object like arrays, but this format is very odd for me. Is there any way to iterate this array like object array?
( [attachments] =>
Array (
[name] => Array (
[0] => test1.png
[1] => test2.png
)
[type] => Array (
[0] => image/png
[1] => image/png
)
[tmp_name] => Array (
[0] => /tmp/phpmFkEUe
[1] => /tmp/phpbLtZRw
)
[error] => Array (
[0] => 0
[1] => 0
)
[size] => Array (
[0] => 9855
[1] => 3002
)
)
)
推荐答案
哇,这确实很奇怪,您是否在标记中使用<input type="file" name="attachments[]" />
?如果您有能力使用唯一的name=
进行更改,则不会有这种奇怪的格式...
Wow, that indeed is odd, are you using <input type="file" name="attachments[]" />
in your markup? If you could afford to change those using unique name=
, you won't have that odd format...
要直接回答您的问题,请尝试:
To directly answer your question, try:
$len = count($_FILES['attachments']['name']);
for($i = 0; $i < $len; $i++) {
$fileSize = $_FILES['attachments']['size'][$i];
// change size to whatever key you need - error, tmp_name etc
}
这篇关于如何迭代PHP $ _FILES数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!