你好朋友开发者,
我有一个关于上传文件的问题,在php中有多个输入。 这是我的代码。
if(isset($_FILES['images'])){
$dateinsert = Carbon::now();
$success = false;
$valid_formats = array("jpg", "png", "jpeg");
$max_file_size = 10000000;
$path = 'var/www/html/test/picture/'; // Upload directory
$count = 0;
$folder = $path . 'picture/user' . '.' . $_SESSION["user"] . "/picture";
$time = date("m/d/y");
if (!file_exists($folder)) {
mkdir($folder, 0777, true);
}
foreach ($_FILES['images']['name'] as $f => $name) {
if ($_FILES['images']['error'][$f] == 4) {
$this->flash->addMessage('ErrorFile', error');
return;
}
if ($_FILES['images']['error'][$f] == 0) {
if ($_FILES['images']['size'][$f] > $max_file_size) {
$this->flash->addMessage('ErrorSize', 'error');
// error
} elseif (!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
$this->flash->addMessage('ErrorFormat', 'error');
// error
} else {
$fichier = strtr($name,
'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
$namea = preg_replace('/([^.a-z0-9]+)/i', '', $fichier);
$namePic = explode('.', $namea);
$name = strtolower(uniqid(rand()) . '_' . $time . '.' . $namePic[1]);
$destination = $folder . "/" . $name;
if (move_uploaded_file($_FILES["images"]["tmp_name"][$f], $destination))
$count++;
$success = true;
if ($success === true) {
$stmt = $this->getDB()->prepare("INSERT INTO insert_annonce
(img_nom,img_taille,img_type,img_desc,date_creat,id_user,id_front)
VALUES(?,?,?,?,?,?,?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $_FILES['images']['size'][$f]);
$stmt->bindParam(3, $namePic[1]);
$stmt->bindParam(4, $destination);
$stmt->bindParam(5, $dateinsert);
$stmt->bindParam(6, $_SESSION['user']);
$stmt->bindParam(7, $last_id);
$result = $stmt->execute();
return $response->withRedirect($this->router->pathFor('home'));
}
}
}
}
}
这是html
<input type="file" name="images[]" class="file" data-show-upload="false" data-show-caption="false" data-show-remove="false" accept="image/jpeg,image/png" data-browse-class="btn btn-blue" data-browse-label="browse">
<input type="file" name="images[]" class="file" data-show-upload="false" data-show-caption="false" data-show-remove="false" accept="image/jpeg,image/png" data-browse-class="btn btn-blue" data-browse-label="browse">
<input type="file" name="images[]" class="file" data-show-upload="false" data-show-caption="false" data-show-remove="false" accept="image/jpeg,image/png" data-browse-class="btn btn-blue" data-browse-label="browse">
<input type="file" name="images[]" class="file" data-show-upload="false" data-show-caption="false" data-show-remove="false" accept="image/jpeg,image/png" data-browse-class="btn btn-blue" data-browse-label="browse">
该脚本可以正常运行,但它会为我加载一个文件而不是我的数据库中的其他文件。
提前感谢您的解释