codice:
include "auth.php";
require "../init.php";
$foto1 = "";
$foto2 = "";
$foto3 = "";
$foto4 = "";
$foto5 = "";
if(is_uploaded_file($_FILES['image_filename1']['tmp_name'])) {
$foto1 = gestisci_upload(1);
}
if(is_uploaded_file($_FILES['image_filename2']['tmp_name'])) {
$foto2 = gestisci_upload(2);
}
if(is_uploaded_file($_FILES['image_filename3']['tmp_name'])) {
$foto3 = gestisci_upload(3);
}
if(is_uploaded_file($_FILES['image_filename4']['tmp_name'])) {
$foto4 = gestisci_upload(4);
}
if(is_uploaded_file($_FILES['image_filename5']['tmp_name'])) {
$foto5 = gestisci_upload(5);
}
$stato = mysql_real_escape_string($_POST['stato']);
$marca = mysql_real_escape_string($_POST['marca']);
$modello = mysql_real_escape_string($_POST['modello']);
$carburante = mysql_real_escape_string($_POST['carburante']);
$cilindrata = mysql_real_escape_string($_POST['cilindrata']);
$versione = mysql_real_escape_string($_POST['versione']);
$anno = mysql_real_escape_string($_POST['anno']);
$prezzo = mysql_real_escape_string($_POST['prezzo']);
$descrizione = mysql_real_escape_string($_POST['descrizione']);
$data = date("Y-m-d");
$insert = "INSERT INTO auto
set stato = '$stato',
marca = '$marca',
modello = '$modello',
carburante = '$carburante',
cilindrata = '$cilindrata',
versione = '$versione',
anno = '$anno',
prezzo = '$prezzo',
descrizione = '$descrizione',
data = '$data',
foto1 = '$foto1',
foto2 = '$foto2',
foto3 = '$foto3',
foto4 = '$foto4',
foto5 = '$foto5'";
$insertresults = mysql_query($insert)
or die(mysql_error());
header('Location: index.php');
exit;
function gestisci_upload($num){
$sFilename = "image_filename".$num;
$image = fopen("image.txt", "a");
fwrite($image, $num);
// gestisco l'upload della foto
$tipi_supportati = array('1','2','3'); //'.gif','.jpeg','.png'
list($width, $height, $type, $attr) = getimagesize($_FILES[$sFilename]['tmp_name']);
if ($type == 1) {
$source = imagecreatefromgif($_FILES[$sFilename]['tmp_name']);
} elseif ($type == 2) {
$source = imagecreatefromjpeg($_FILES[$sFilename]['tmp_name']);
} elseif ($type == 3) {
$source = imagecreatefrompng($_FILES[$sFilename]['tmp_name']);
} else {
//$CONTENT .= "<div style=\"color:red;\">Formato immagine non supportato</div>";
return;
}
$time = time();
$imgfile = "../upload/".$time."_".$num.".jpg";
$thumbfile = "../upload/thumbs/".$time."_".$num.".jpg";
// faccio la thumb
/*
if ($width >= $height) {
$ratio = 130/$width;
} else {
$ratio = 160/$height;
}
*/
$ratio = ($width/200 >= $height/200) ? 200 / $width : 200 / $height;
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
//$temp = imagecreatetruecolor($newwidth, $newheight);
$temp = imagecreatetruecolor(200, 200);
$background = imagecolorallocate($temp, 250, 240, 213);
imagefilledrectangle($temp, 0, 0, 200, 200, $background);
imagecopyresampled($temp, $source, (100-$newwidth/2), (100-$newheight/2), 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($temp,$thumbfile,90);
// faccio la grande
if ($width <= 500) {
$temp = imagecreatetruecolor($width, $height);
imagefilledrectangle($temp, 0, 0, $width, $height, $background);
imagecopyresampled($temp, $source, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($temp,$imgfile,90);
} else {
$ratio = 500/$width;
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
$temp = imagecreatetruecolor($newwidth, $newheight);
imagefilledrectangle($temp, 0, 0, $newwidth, $newheight, $background);
imagecopyresampled($temp, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($temp,$imgfile,90);
}
fclose($image);
return $time."_".$num;
}
?>