我有多个输入字段具有相同的名称。他们看起来像:PHP通过输入字段名称相同的循环,全部上传
<input type="hidden" class="image-hidden" name="image-to-upload[]" />
<input type="hidden" class="image-hidden" name="image-to-upload[]" />
<input type="hidden" class="image-hidden" name="image-to-upload[]" />
<input type="hidden" class="image-hidden" name="image-to-upload[]" />
...
...
我这个代码上传:
<?php
if(isset($_POST['new-blogpost'])) {
$img = $_POST['image-to-upload'][0];
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = 'image.jpg';
$success = file_put_contents($file, $data);
};
?>
的问题是,这个代码将只上载第一个输入字段的画面。
如何重写我的代码以上传所有输入字段? (我知道我必须在这种情况下给我的文件唯一的名称,但那不是我的问题。我想知道如何告诉PHP它必须遍历所有输入字段并执行上传。 !前进
只需循环变量'$ _POST ['image-to-upload']'。它是一个数组,因此您可以查看其中的所有项目。 – Dekel
好的,谢谢。但我不知道该怎么做:/试图找出它,但我不明白 – Maxischl
在谷歌搜索“通过数组的PHP循环”给出了第一个结果:http://php.net /manual/en/control-structures.foreach.php – Dekel