我有一个基本的脚本,允许用户批量上传产品到数据库。第一步是上传CSV文件。上传文件后,脚本将显示一个页面,使用户能够查看每个产品,并为每个产品添加一张或多张照片。
我使用的HTML与以下内容类似:
<input type="file" class="form-control" name="photos[]" id="photos" multiple>此HTML输入针对每个产品显示一次,全部显示在一个HTML表单中。
当我在服务器端收到提交时,它正在将来自所有HTML输入的所有产品照片合并到一个数组中。问题是,我不知道哪些照片属于哪些产品。
有没有办法解决这个问题,这样我就可以区分照片了?每个产品可能有多张照片,我想从它自己的选择框中上传每一组照片。
发布于 2015-05-17 08:19:48
在<input>名称中使用多维数组:
<input type="file" class="form-control" name="photos[productid][]" id="photos" multiple>其中productid是每个产品的产品ID。
发布于 2015-05-17 08:44:25
我假设您的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-05-17 09:00:48
就像他们上面说的那样,使用多维数组。
该数组将包含两个数组: ids。
在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循环来获取数据。
祝好运
https://stackoverflow.com/questions/30282017
复制相似问题