Codice PHP:
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$db = new PDO('mysql:dbname=prodotti;host=localhost', 'root', 'root');
if (isset($_POST['submit'])):
$nome = $_POST['nome'];
$descrizione = $_POST['descrizione'];
$prezzo = $_POST['prezzo'];
$new_post = "INSERT INTO `prodotti` ( `nome`, `descrizione`, `prezzo`) VALUES (?, ?, ?)";
$stmt = $db->prepare($new_post);
$stmt->execute(array($nome, $descrizione, $prezzo));
$new_post_id = $db->lastInsertId();
// now start processing the images
$image_sql = "INSERT INTO `immagini` (`id_prodotto`, `nome_immagine`) VALUES (?, ?)";
$stmt = $db->prepare($image_sql);
for ($i = 1; $i <= 2; $i++) {
// you need to add some code to validate, move and rename the files
ini_set("memory_limit","256M");
if(isset($_FILES['immagine1'])){
$imagename = $_FILES['immagine1']['name'];
$source = $_FILES['immagine1']['tmp_name'];
$target = "../../img/prodotti/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "../../img/prodotti/" . $imagepath; // questo è il nuovo file che salvi
$file = "../../img/prodotti/" . $imagepath; // questo è il file originale
list($width, $height) = getimagesize($file);
$modwidth = 1024; // dimensione immagine grande
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 90);
$save = "../../img/prodotti/miniatura-" . $imagepath; // questo è il nuovo file che salvi
$file = "../../img/prodotti/" . $imagepath; // questo è il file originale
list($width, $height) = getimagesize($file);
$max_w = 150;
$max_h = 113;
// area utile e offset
if ($width>$height*$max_w/$max_h) {
$a_h = $height;
$a_w = $a_h*$max_w/$max_h;
$delta_x = floor(($width-$a_w)/2);
$delta_y = 0;
}
else {
$a_w = $width;
$a_h = $a_w*$max_h/$max_w;
$delta_x = 0;
$delta_y = floor(($height-$a_h)/2);
}
$tn = imagecreatetruecolor($max_w, $max_h);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, $delta_x, $delta_y, $max_w, $max_h, $a_w, $a_h);
imagejpeg($tn, $save, 90);
}
if(isset($_FILES['immagine2'])){
$imagename = $_FILES['immagine2']['name'];
$source = $_FILES['immagine2']['tmp_name'];
$target = "../../img/prodotti/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$save = "../../img/prodotti/" . $imagepath; // questo è il nuovo file che salvi
$file = "../../img/prodotti/" . $imagepath; // questo è il file originale
list($width, $height) = getimagesize($file);
$modwidth = 1024; // dimensione immagine grande
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 90);
$save = "../../img/prodotti/miniatura-" . $imagepath; // questo è il nuovo file che salvi
$file = "../../img/prodotti/" . $imagepath; // questo è il file originale
list($width, $height) = getimagesize($file);
$max_w = 150;
$max_h = 113;
// area utile e offset
if ($width>$height*$max_w/$max_h) {
$a_h = $height;
$a_w = $a_h*$max_w/$max_h;
$delta_x = floor(($width-$a_w)/2);
$delta_y = 0;
}
else {
$a_w = $width;
$a_h = $a_w*$max_h/$max_w;
$delta_x = 0;
$delta_y = floor(($height-$a_h)/2);
}
$tn = imagecreatetruecolor($max_w, $max_h);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, $delta_x, $delta_y, $max_w, $max_h, $a_w, $a_h);
imagejpeg($tn, $save, 90);
}
// add the files to the db
$file_name = $_FILES['immagine1']['name'];
$stmt->execute(array($new_post_id, $file_name));
$file_name = $_FILES['immagine2']['name'];
$stmt->execute(array($new_post_id, $file_name));
}
if (@mysql_query($sql)) {
echo "<script>alert('Inserimento eseguito con successo.');</script>";
echo "<script>document.location='../menu_amministrazione.php'</script>";
} else {
echo "<script>alert('Impossibile eseguire l'inserimento.');</script>";
echo "<script>document.location='../menu_amministrazione.php'</script>";
}
?>
<?php
else:
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Prodotti - Inserisci</title>
<link href="../stile.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function validateForm()
{
var x=document.forms["modulo_inserisci"]["nome"].value;
if (x==null || x=="")
{
alert("Inserisci il nome.");
return false;
}
var x=document.forms["modulo_inserisci"]["descrizione"].value;
if (x==null || x=="")
{
alert("Inserisci la descrizione.");
return false;
}
var x=document.forms["modulo_inserisci"]["prezzo"].value;
if (x==null || x=="")
{
alert("Inserisci il prezzo.");
return false;
}
var x=document.forms["modulo_inserisci"]["immagine1"].value;
if (x==null || x=="")
{
alert("Seleziona l'immagine 01 da caricare.");
return false;
}
var x=document.forms["modulo_inserisci"]["immagine2"].value;
if (x==null || x=="")
{
alert("Seleziona l'immagine 02da caricare.");
return false;
}
}
</script>
</head>
<body>
<table width="980" border="0" align="center" cellpadding="1" cellspacing="2" class="border">
<tr>
<td height="20" colspan="2" align="right" id="voce"><a href="../menu_amministrazione.php">Home</a> - <a href="../logout.php">Logout</a></td>
</tr>
<tr>
<td height="68" colspan="2"><img src="../immagini/header.jpg" width="980" height="100" alt="logo" /></td>
</tr>
<form action="" method="post" enctype="multipart/form-data" name="modulo_inserisci" id="modulo_inserisci" onsubmit="return validateForm()">
<tr>
<td colspan="2" valign="middle"><table width="100%" border="0">
<tr>
<td><table width="100%" border="0" cellspacing="0">
<tr>
<td colspan="2" id="intestazione"><div align="center">Prodotti - Inserisci</div></td>
</tr>
<tr>
<td colspan="2"><br /></td>
</tr>
<tr>
<td width="50%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Nome: </div></td>
<td width="50%" id="voce"><div align="left"><input name="nome" type="text" id="nome" /></div></td>
</tr>
<tr>
<td width="50%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Descrizione: </div></td>
<td width="50%" id="voce"><div align="left"><textarea name="descrizione" id="descrizione" cols="25" rows="10"></textarea></div></td>
</tr>
<tr>
<td width="50%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Prezzo: </div></td>
<td width="50%" id="voce"><div align="left"><input name="prezzo" type="text" id="prezzo" /></div></td>
</tr>
<tr>
<td width="50%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Immagine 01: </div></td>
<td width="50%" id="voce"><input name="immagine1" id="immagine" size="30" type="file" /></div></td>
</tr>
<tr>
<td width="50%" colspan="1" id="voce" style="font-weight:bold"><div align="right">Immagine 02: </div></td>
<td width="50%" id="voce"><input name="immagine2" id="immagine" size="30" type="file" /></div></td>
</tr>
<tr>
<td width="50%" colspan="1" id="voce" style="font-weight:bold"><div align="right"></div></td>
<td width="50%" id="voce"><div align="left"><br /> = A capo</div></td>
</tr>
<tr>
<td colspan="2"><br /></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input name="submit" type="submit" id="button_ok" value="Invia" /></div></td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
</form>
<tr>
<td height="6" colspan="2" bgcolor="#000000"></td>
</tr>
</table>
</body>
</html>
<?php endif; ?>
Finalmente riesco a fare un upload multiplo con lo stesso form, ma ho 2 problemi, il primo è che nel DB mi scrive doppio il record delle immagini che carico, quindi 2 immagini, mi ritrovo con 4 record, 2 riferiti all'immagine 01, e 2 riferiti all'immagine 02, dov'è secondo voi l'errore nel codice?