多个上传循环

时间:2011-12-21 16:53:41

标签: php file loops upload

出于某种原因,我必须将所有输入字段放入数组中。因为输入是动态添加到用户的。

当用户点击添加按钮时,将为上传图像生成以下内容

<input type="file" name="product['+n+'][gthumbnail][]"  />

但我不知道如何循环可用数组(已填充) 。我甚至无法计算数组大小。这些是我的工作代码希望每个专家都可以指出我做的错误

//loop for image file upload
foreach($product as $productcount){

....
....
....

    //this is what i have now ,and it is not working,error saying the gthumbnail is undefined
    for($i=0;$i<sizeof($_FILES['gthumbnail']);$i++){

        $m=uploadFile($_FILES['gthumbnail'][$i]);

    }


}//End of product loop

的print_r($ FILES)

Array ( 
[product] => Array ( [name] => Array ( [1] => Array ( [thumbnail] => [gthumbnail] => Array ( [0] => [1] => [2] => ) [gphoto] => Array ( [0] => [1] => [2] => ) ) ) [type] => Array ( [1] => Array ( [thumbnail] => [gthumbnail] => Array ( [0] => [1] => [2] => ) [gphoto] => Array ( [0] => [1] => [2] => ) ) ) [tmp_name] => Array ( [1] => Array ( [thumbnail] => [gthumbnail] => Array ( [0] => [1] => [2] => ) [gphoto] => Array ( [0] => [1] => [2] => ) ) ) [error] => Array ( [1] => Array ( [thumbnail] => 4 [gthumbnail] => Array ( [0] => 4 [1] => 4 [2] => 4 ) [gphoto] => Array ( [0] => 4 [1] => 4 [2] => 4 ) ) ) [size] => Array ( [1] => Array ( [thumbnail] => 0 [gthumbnail] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [gphoto] => Array ( [0] => 0 [1] => 0 [2] => 0 ) ) ) ) )

1 个答案:

答案 0 :(得分:0)

为什么不尝试使用单维数组而不是name="product['+n+'][gthumbnail][]"。看起来很复杂。

可能会使您的工作更轻松,

 <form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="Send files" />
</form>

提交上述表单后,数组$_FILES['userfile'], $_FILES['userfile']['name']$_FILES['userfile']['size']将被初始化。

当register_globals打开时,上传文件的全局变量也会被初始化。其中每个都是提交文件的适当值的数字索引数组。

例如,假设提交了文件名/home/test/review.html和/home/test/xwp.out。在这种情况下,$ _FILES ['userfile'] ['name'] [0]将包含值review.html,$ _FILES ['userfile'] ['name'] [1]将包含值xwp.out 。同样,$ _FILES ['userfile'] ['size'] [0]将包含review.html的文件大小,依此类推。

$_FILES['userfile']['name'][0], $_FILES['userfile']['tmp_name'][0], $_FILES['userfile']['size'][0], and $_FILES['userfile']['type'][0] are also set.