我有以下表格(我放了4个文件输入标签),我想将这4张图片保存到名为“mybase”的数据库中。
以下是表格:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Envoyer une image</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
label {
display:block;
width:150px;
float:left;
}
</style>
</head>
<body>
<h1>Envoyer une image</h1>
<form enctype="multipart/form-data" action="traitement.php" method="post">
<p>
<?php
$serveur_db = 'localhost';
$user_db = 'root';
$pass_db = '';
$base_db = 'mybase';
$query = "SELECT * FROM pays";
$res = $conn->query($query);
?>
<select name="truc">
<?php
while($line = $res->fetch()){
echo '<option value="'.$line['id_pays'].'">'.$line['nom_pays'].'</option>';
}
?>
</select>
<br>
<label for="nom">Picture1 : </label><input type="text" name="nom" id="nom" /><br />
<label for="image">Image : </label><input type="file" name="image" id="pic1" /><br />
<label for="nom">Picture2 : </label><input type="text" name="nom" id="nom" /><br />
<label for="image">Image : </label><input type="file" name="image" id="pic2" /><br />
<label for="nom">Picture3 : </label><input type="text" name="nom" id="nom" /><br />
<label for="image">Image : </label><input type="file" name="image" id="pic3" /><br />
<label for="nom">Picture4 : </label><input type="text" name="nom" id="nom" /><br />
<label for="image">Image : </label><input type="file" name="image" id="pic4" /><br />
<br>
<label for="validation">Valider : </label><input type="submit" name="validation" id="validation" value="Envoyer" />
</p>
</form>
</body>
</html>
这是traitement.php文件:
<?php
if(isset($_POST['validation'])) {
if(!is_uploaded_file($_FILES['image']['tmp_name']))
echo 'Un problème est survenu durant l opération. Veuillez réessayer !';
else {
$extensions = array('/png', '/gif', '/jpg', '/jpeg');
//récupère la chaîne à partir du dernier / pour connaître l'extension
$extension = strrchr($_FILES['image']['type'], '/');
//vérifie si l'extension est dans notre tableau
if(!in_array($extension, $extensions))
echo 'Vous devez uploader un fichier de type png, gif, jpg, jpeg.';
else {
define('MAXSIZE', 300000);
if($_FILES['image']['size'] > MAXSIZE)
echo 'Votre image est supérieure à la taille maximale de '.MAXSIZE.' octets';
else {
try {
$bdd = new PDO('mysql:host=localhost;dbname=mybase', 'root', '');
} catch (Exception $e) {
exit('Erreur : ' . $e->getMessage());
}
$image = file_get_contents($_FILES['image']['tmp_name']);
$selected_val = $_POST['truc'];
$req = $bdd->prepare("INSERT INTO images(id_pays_img,nom,img, extension) VALUES(:id_pays_img, :nom,:image, :type)");
$req->execute(array(
'id_pays_img'=>$_POST['truc'],
'nom' => $_POST['nom'],
//'description' => $_POST['description'],
'image' => $image,
'type' => $_FILES['image']['type']
));
echo 'operation succesed !';
}
}
}
}
?>
此代码适用于一张图片,但我不知道如何同时保存4张图片。
答案 0 :(得分:0)
您可以在表单中使用不同的名称,例如image1
,image2
...或带有image[]
的数组。在后一种情况下,您将需要注意如何访问子元素,如下所述: