Код:
<?php
//connect to the database
$link = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("reklama", $link)
or die (mysql_error());
//make variables available
$advert_message = $_POST['advert_message'];
$advert_user = $_POST['advert_user'];
if (empty($_FILES['image_filename']['name'])) {
$_FILES['image_filename']['name'] == "<img src=\"C:/Program Files/apachefriends/xampp/htdocs/reklama/pic.gif\">";
$image_tempname = $_FILES['image_filename']['name'];
} else {
$image_tempname = $_FILES['image_filename']['name'];
}
$advert_type = $_POST['advert_type'];
$advert_agree = $_POST['advert_agree'];
$today = date("Y-m-d H:i:s");
//upload image and check for image type
$ImageDir ="C:/Program Files/apachefriends/xampp/htdocs/reklama/images/";
$ImageThumb = $ImageDir . "thumbs/";
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
$ImageName)) {
//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);
if ($type > 3) {
echo "Ñúæåëÿâàìå íî ôàéëà òðÿáâà äà áúäå îò òèï JPEG,Gif " .
"èëè PNG.<br>";
echo "Ìîëÿ îïèòàéòå îòíîâî.";
} else {
//image is acceptable; ok to proceed
//insert info into image table
$advert_message = trim($_POST['advert_message']);
if(empty($advert_message)) { $error .= "Ìîëÿ+âúâåäåòå+îáÿâà%21%0D%0A"; }
if(empty($_POST['advert_user'])) { $error .="Ìîëÿ+âúâåäåòå+îò+êîé+å+îáÿâàòà%21%0D%0A"; }
if(empty($_POST['advert_type'])) { $error .="Ìîëÿ+âúâåäåòå+òèï+íà+îáÿâàòà%21%0D%0A"; }
if(empty($_POST['advert_agree'])) { $error .="Òðÿáâà+äà+ñòå+ñúãëàñíè+ñúñ+óñëîâèÿòà%21%0D%0A"; }
if (empty($error)) {
$insert = "INSERT INTO advert_post
(advert_id, advert_message, advert_user, advert_date, advert_type, advert_agree)
VALUES
(NULL, '$advert_message', '$advert_user', '$today', '$advert_type', 'advert_agree')";
} else {
header("location:pusni_obqva.php?action=add&error=" . $error);
}
if (isset($insert) && !empty($insert)) {
echo "<!--" .$insert. "--!>";
?>
<?php
}
$insertresults = mysql_query($insert)
or die(mysql_error());
$lastpicid = mysql_insert_id();
//change the following line:
$newfilename = $ImageDir . $lastpicid . ".jpg";
if ($type == 2) {
rename($ImageName, $newfilename);
} else {
if ($type == 1) {
$image_old = imagecreatefromgif($ImageName);
} elseif ($type == 3) {
$image_old = imagecreatefrompng($ImageName);
}
//"convert" the image to jpg
$image_jpg = imagecreatetruecolor($width, $height);
imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0,
$width, $height, $width, $height);
imagejpeg($image_jpg, $newfilename);
imagedestroy($image_old);
imagedestroy($image_jpg);
}
$newthumbname = $ImageThumb . $lastpicid . ".jpg";
//get the dimensions for the thumbnail
$thumb_width = 100;
$thumb_height = 75;
//create the thumbnail
$largeimage = imagecreatefromjpeg($newfilename);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0,
$thumb_width, $thumb_height, $width, $height);
imagejpeg($thumb, $newthumbname);
imagedestroy($largeimage);
imagedestroy($thumb);
$url = "location: viewadvert.php?t=" . $lastpicid;
header($url);
}
}
?>