PHP多文件上传使用多个多个文件输入


PHP Multiple File Upload using Several Multiple File Inputs

我有一个允许用户批量上传产品到数据库的基本脚本。第一步是上传CSV文件。文件上传后,脚本显示一个页面,用户可以查看每个产品,并为每个产品添加一张或多张照片来上传。

我使用的HTML类似如下:

<input type="file" class="form-control" name="photos[]" id="photos" multiple>

此HTML输入在每个产品中显示一次,全部在一个HTML表单中。

当我在服务器端收到提交时,它正在将来自所有HTML输入的所有产品照片合并到一个数组中。问题是,我不知道哪些照片属于哪些产品。

有什么办法可以解决这个问题,让我可以区分照片吗?每个产品可能有多张照片,我想从它自己的选择框中上传每一组。

在您的<input>名称中使用多维数组:

<input type="file" class="form-control" name="photos[productid][]" id="photos" multiple>

其中productid为每个产品的产品ID

就像上面所说的那样,使用多维数组。
该数组将包含2个数组:[id][图像]。
在HTML中应该是这样的:

the loop you have {
<input type="file" name="photos[<?php echo $id ?>][<?php echo $i ?>]" class="form-control">
}

下面是接收数组的方法:

<?php 
foreach ($_POST["photos"] as $id) {
    foreach ($id as $photo) {
        $sql[$id][$photo] = "INSERT INTO tableName (Photo, IDProduct) VALUES($photo, $id)";
    }
}
?>

这只是一个例子,在这个例子中可能有一些错误,但是你必须用多维数组来做这个,然后使用foreach循环来获取数据。
祝你好运

我假定您的PHP脚本是基于CSV条目生成表单的。

通过在photos[]表单数组中输出某种唯一标识符,您将能够将照片连接到相关条目。

arbirtrary例子:

<?php
$csv_entries = array( 'one', 'two', 'three' );
foreach( $csv_entries as $csv_unique_identifier ) : ?>
<input type="file" class="form-control" name="photos_<?php echo $csv_unique_identifier; ?>[]" id="photos" multiple>
<?php endforeach; ?>
编辑:

name="photos[$csv_unique_identifier]"改为photos_$csv_unique_identifier[]"

如果您从提交的表单中选择print_r( $__FILES__ ),您将得到如下内容(确保一直向下滚动以完整查看示例):

Array
(
    [photos_one] => Array
        (
            [name] => Array
                (
                    [0] => one-2.txt
                    [1] => one-1.txt
                )
            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/phpsXiiL3
                    [1] => /tmp/phpFW4ki3
                )
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )
            [size] => Array
                (
                    [0] => 4
                    [1] => 4
                )
        )
    [photos_two] => Array
        (
            [name] => Array
                (
                    [0] => two-2.txt
                    [1] => two-1.txt
                )
            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/phpgouoP2
                    [1] => /tmp/phpRfNsm2
                )
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )
            [size] => Array
                (
                    [0] => 4
                    [1] => 4
                )
        )
    [photos_three] => Array
        (
            [name] => Array
                (
                    [0] => 
                )
            [type] => Array
                (
                    [0] => 
                )
            [tmp_name] => Array
                (
                    [0] => 
                )
            [error] => Array
                (
                    [0] => 4
                )
            [size] => Array
                (
                    [0] => 0
                )
        )
)