我正在尝试在我的系统中进行简单的上传,只需使用 multiple="true"
输入属性。
实际上,我有 15 个文件输入,它们都将文件名重命名为:'id.in.the.bd'_'number'.jpg(例如:65_1.jpg)。像这样:
HTML
// inside a form
<?php for ($x = 1; $x <= 15; $x++) { ?>
<input name="foto_<?php echo $x; ?>" type="file" id="foto" multiple="true" />
<?php } ?>
PHP
if ($_GET['action'] == "insert") {
//sql insert
// do a consult to find the $id
$sql = mysql_query("SELECT * FROM imoveis ORDER BY id DESC LIMIT 1", $link);
$result = mysql_fetch_array($sql, MYSQL_ASSOC);
$id = $result['id'];
for ($x = 1; $x <= 15; $x++) {
$file = $_FILES["foto_" . $x];
$path = '../../images/galeria/' . $id . '_' . $x . '.jpg';
move_uploaded_file($file["tmp_name"], $path);
}
如何轻松更改代码以使用 multiple="true"
?如果我只是将 input
更改为 multiple="true"
,他只需上传所选的第一张图片。
最佳答案
将输入标签更改为
<input name="foto[]" id="foto_1" type="file" />
在 PHP 中,这些文件可以通过 $_FILES['foto']['name'] 数组访问,如下所示:
$_FILES['foto']['name'][0] for file 1
$_FILES['foto']['name'][1] for file 2
等等..
关于php - MySQL中简单上传多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19573717/