Codice PHP:
<?php require_once('ConnessioneDB.php');
$abpathoriginali = $_SERVER['DOCUMENT_ROOT'] . "uploaded/originali" ;
$abpathminia = $_SERVER['DOCUMENT_ROOT'] . "uploaded/miniature" ;
$abpathgrandi = $_SERVER['DOCUMENT_ROOT'] . "uploaded/grandi" ;
$size = "2000000"; //limita la grandezza del file
$dim = 80; // DIMENSIONI MINIATURE
$dimGRANDI = 500; // DIMENSIONI GRANDI
$cert1 = "image/pjpeg"; //Jpeg type 1
$cert2 = "image/jpeg"; //Jpeg type 2
$cert3 = "image/gif"; //Gif type
$img1 = $_FILES['img1']['name'];
$descrizione = $_POST['descrizione'];
if ($_FILES['img1']['name'] == "") {
$log= "non è stato selezionato nessun file";
}
$nome = $_FILES['img1']['name'];
$nomefile = $abpathoriginali . "/" . $nome;
if (!file_exists($nomefile)) {
if ($_FILES['img1']['size'] < $size) {
if ($_FILES['img1']['type'] == $cert1 or $_FILES['img1']['type'] == $cert2 or $_FILES['img1']['type'] == $cert3) {
if ($_FILES['img1']['name'] != "") {
if ( $_POST['testo'] == '' ) {
$testo = "img"; } else { $testo = $_POST['testo']; }
$name = $_FILES['img1']['name'];
$uploadfile = $abpathoriginali."/". $_FILES['img1']['name'];
$NUOVO = $abpathoriginali."/". $name;
move_uploaded_file($_FILES['img1']['tmp_name'], $uploadfile);
if (file_exists($NUOVO)) {
$log= "Il file è stato caricato correttamente";
mysql_select_db ($DB);
$sql = "INSERT into tabella (nomefoto, descrizione) VALUES ('$img1' , '$descrizione')";
mysql_query($sql) or die("Errore: " . mysql_error() . " - query[{$sql}]");
list($width, $height, $type, $attr) = getimagesize($abpathoriginali . "/" . $_FILES['img1']['name']);
// CONTROLLO SE è UN JPG
if ($_FILES['img1']['type'] == $cert1 or $_FILES['img1']['type'] == $cert2) {
// Creo la versione 80*n dell'immagine (thumbnail)
$numero = ($dim/$width);
$thumb = imagecreatetruecolor($width*$numero, $height*$numero);
$source = imagecreatefromjpeg($abpathoriginali . "/" . $_FILES['img1']['name']);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width*$numero, $height*$numero, $width, $height);
// Salvo l'immagine ridimensionata
imagejpeg($thumb, $abpathminia . "/" . $_FILES['img1']['name'], 65);
// Creo la versione 500*n dell'immagine (thumbnail)
if ($width > $dimGRANDI ) {
$numero = ($dimGRANDI/$width);
$thumb = imagecreatetruecolor($width*$numero, $height*$numero);
$source = imagecreatefromjpeg($abpathoriginali . "/" . $_FILES['img1']['name']);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width*$numero, $height*$numero, $width, $height);
// Salvo l'immagine ridimensionata
imagejpeg($thumb, $abpathgrandi . "/" . $_FILES['img1']['name'], 65);
} else {
$thumb = imagecreatetruecolor($width, $height);
$source = imagecreatefromjpeg($abpathoriginali . "/" . $_FILES['img1']['name']);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width, $height, $width, $height);
// Salvo l'immagine ridimensionata
imagejpeg($thumb, $abpathgrandi . "/" . $_FILES['img1']['name'], 65);
}
} elseif ($_FILES['img1']['type'] == $cert3) {
// CONTROLLO SE è UNA GIF
$numero = ($dim/$width);
$thumb = imagecreatetruecolor($width*$numero, $height*$numero);
$source = imagecreatefromgif($abpathoriginali . "/" . $_FILES['img1']['name']);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width*$numero, $height*$numero, $width, $height);
// Salvo l'immagine ridimensionata
imagegif($thumb, $abpathminia . "/" . $_FILES['img1']['name'], 65);
if ($width > $dimGRANDI ) {
// Creo la versione 700*n dell'immagine (thumbnail)
$numero = ($dimGRANDI/$width);
$thumb = imagecreatetruecolor($width*$numero, $height*$numero);
$source = imagecreatefromgif($abpathoriginali . "/" . $_FILES['img1']['name']);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width*$numero, $height*$numero, $width, $height);
// Salvo l'immagine ridimensionata
imagegif($thumb, $abpathgrandi . "/" . $_FILES['img1']['name'], 65);
} else {
$thumb = imagecreatetruecolor($width, $height);
$source = imagecreatefromgif($abpathoriginali . "/" . $_FILES['img1']['name']);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width, $height, $width, $height);
// Salvo l'immagine ridimensionata
imagegif($thumb, $abpathgrandi . "/" . $_FILES['img1']['name'], 65);
}
} else {
$log= "Puoi inserire solo immagini visibili da un browser: JPG o GIF";
}
} else {
$log= "Il file non è stato caricato per motivi sconosciuti";
}
}
}
} else {
$log= "ATTENZIONE.
Il file è troppo grande";
}
} else {
$log= "ATTENZIONE.
Il file che hai inserito è già stato caricato da te, per questo o un altro abstract.";
}
?>