3

我正在像这样向 php 发送一组文件

<input type="file" name="image_name[]" />

但是,例如,当我要求 2 个文件并 foreach 它时,它返回的文件信息不是 2 个数组,而是 5 个信息数组,每个数组都是文件规范。

Foreach 循环:

foreach($image as $key => $oneImage){
    print_r($oneImage);
    echo "<br />";
}

输出:

Array ( [0] => 3.jpg [1] => 5585_387497301371274_1740842451_n.png ) 
Array ( [0] => image/jpeg [1] => image/png ) 
Array ( [0] => /tmp/php8lgHOu [1] => /tmp/phpJOpJye ) 
Array ( [0] => 0 [1] => 0 ) 
Array ( [0] => 56405 [1] => 504664 ) 

如果无法将其作为我想要的数组,我将如何捕获例如第三个数组的 tmp_name ?(无需进行多个 foreach 循环)

4

1 回答 1

1

当您上传文件数组时,返回的数组的结构有点奇怪,在我看来不是最直观的。

我的解决方案是使用 for 循环而不是 foreach

我的 HTML:

<input type="file" name="image_name[]" />
<input type="file" name="image_name[]" />
<input type="file" name="image_name[]" />

我的PHP:

<?php
$count = count($_FILES['image_name']['error']);
for ($i = 0; $i < $count; $i++)
{
    // image handling code here
    if ($_FILES['image_name']['error'][$i] ==  0)
    {
        move_uploaded_file($_FILES['image_name']['tmp_name'][$i], 'whatever.jpg');
    }
}
于 2013-06-15T19:55:49.097 回答