Foreach并保存文件multiupload?

时间:2013-10-11 20:16:19

标签: php multidimensional-array foreach

我有多上传文件格式,发布的时候我有这样的东西

$_FILES['slike']=Array( 

[name] => Array ( [0] => 64_maska.png [1] => 35proba.jpg ) 
[type] => Array ( [0] => image/png [1] => image/jpeg ) 
[tmp_name] => Array ( [0] => /tmp/phpMgwdZD [1] => /tmp/phpzgEGyW ) 
[error] => Array ( [0] => 0 [1] => 0 )
[size] => Array ( [0] => 30389 [1] => 84375 )
 )

现在我想要预先使用这个数组来获得类似的东西

foreach ($_FILES['slike'] as $slika)
{

$source=$_FILES['slike']['tmp_name'];
$newimage=$_FILES['slike']['name'];
$path = "uploads/";
move_uploaded_file($source, $path.$newimage)

}

我知道我在foreach中有错误,但不知道如何修复,也许我必须制作两个新阵列才能完成此任务?我不知道如何从多维数组中提取不同的值:*(

2 个答案:

答案 0 :(得分:2)

您可以使用for循环实现此目的:

for ($i=0; $i < count($_FILES['slike']['name']); $i++) {

    $source=$_FILES['slike']['tmp_name'][$i];
    $newimage=$_FILES['slike']['name'][$i];
    $path = "uploads/";
    move_uploaded_file($source, $path.$newimage)
}

答案 1 :(得分:2)

使用for

而不是foreach循环
$path = "uploads/";
foreach ($_FILES['slike']['name'] as $i => $name) {
    $type = $_FILES['slike']['type'][$i];
    move_uploaded_file($_FILES['slike']['tmp_name'][$i], $path . $name)
}