PHP多文件上传

时间:2012-04-23 16:18:09

标签: php file-upload

文件uplods的HTML:

 <form enctype="multipart/form-data" action="" method="POST">
                <br/>Upload Featured Image: <input name="imagefiles" type="file" /><br/>
                <br/>Upload Gallery Image 1: <input name="imagefiles" type="file" /><br/>
                <br/>
                <input type="submit" name="submit" value="Add Product" />

</form>

要处理上传,我这样做:

$imagefiles = $_FILES['imagefiles'];
               foreach ($imagefiles['name'] as $key => $value) ----> [Line 25 in file]
                {


                }

但是,我收到了这个错误:

Warning: Invalid argument supplied for foreach() in /var/www/html/addProductForm.php on line 25 (Edit)

1 个答案:

答案 0 :(得分:1)

您没有使用正确的参数名称。 您必须添加[]以使输入成为数组,否则最后一个元素将覆盖具有相同名称的前一个元素。

尝试以下HTML:

<form enctype="multipart/form-data" action="" method="POST">
   <br/>Upload Featured Image: <input name="imagefiles[]" type="file" /><br/>
   <br/>Upload Gallery Image 1: <input name="imagefiles[]" type="file" /><br/>
   <br/>
   <input type="submit" name="submit" value="Add Product" />
</form>