【问题标题】:PHP Multiple File Upload using Several Multiple File Inputs使用多个多文件输入的 PHP 多文件上传
【发布时间】:2015-05-17 00:13:27
【问题描述】:

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

我正在使用类似于以下内容的 HTML:

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

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

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

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

【问题讨论】:

    标签: php html


    【解决方案1】:

    在您的 &lt;input&gt; 名称中使用多维数组:

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

    productid 是每个产品的产品 ID。

    【讨论】:

    • 在第一次回答时对此表示赞同。有关更多详细信息,请参阅下面的答案,其中显示了结果 $_FILES 数组的示例,注意:"photos[id][]""photos_*ID*[]" 的工作方式相同,除了您可以使用"photos[id][]" 轻松提取“productid”,只需使用索引键即可。
    【解决方案2】:

    就像他们上面所说的,使用多维数组。
    该数组将包含 2 个数组:[ids][the images]。
    在 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 循环获取数据。
    祝你好运

    【讨论】:

    • 您好 Elomar,这适用于单个文件选择,但是在整个表单中多次选择多个文件呢?
    • 那么您有产品,并且您可以为每个产品上传多个图片?
    【解决方案3】:

    我假设您的 PHP 脚本正在根据 CSV 条目生成表单。

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

    任意示例:

    <?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
                    )
            )
    )
    

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2023-02-12
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多