Forum Moderators: coopster
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'dba';
$database = 'gallery';
$table = 'paintings';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
function sql_safe($s)
{
if (get_magic_quotes_gpc())
$s = stripslashes($s);
return mysql_real_escape_string($s);
}
//if ($_SERVER['REQUEST_METHOD'] == 'POST')
if(isset($_POST['upload']))
{
$title = trim(sql_safe($_POST['title']));
$artistid = trim(sql_safe($_POST['txtartistid']));
$description = trim(sql_safe($_POST['txtdescription']));
$category = trim(sql_safe($_POST['category']));
$price = trim(sql_safe($_POST['txtprice']));
$height = trim(sql_safe($_POST['txtheight ']));
$height_scale = trim(sql_safe($_POST['heightscale']));
$width = trim(sql_safe($_POST['txtwidth']));
$width_scale = trim(sql_safe($_POST['widthscale']));
$frame = trim(sql_safe($_POST['txtframe']));
$photographer = trim(sql_safe($_POST['txtphotographer']));
$scale=trim(sql_safe($_POST['scale']));
@list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']); // Get image type. We use @ to omit errors
if ($imtype == 3) // cheking image type
$ext = "png"; // to use it later in HTTP headers
elseif ($imtype == 2)
$ext = "jpeg";
elseif ($imtype == 1)
$ext = "gif";
else
$msg = 'Error: unknown file format';
if (!isset($msg)) // If there was no error
{
$msg = 'Success: image uploaded';
$data = file_get_contents($_FILES['photo']['tmp_name']);
$data = mysql_real_escape_string($data);
// Preparing data to be used in MySQL query
mysql_query("INSERT INTO paintings
SET p_painting_artist_id= '$artistid',
p_painting_description = '$description',
p_painting_category = '$category',
p_painting_price = '$price',
p_painting_height = '$height',
p_painting_width = '$width',
p_painting_frame = '$frame',
p_painting_photographer = '$photographer',
p_painting_tags = '',
p_painting_imagetype = '$ext',
p_painting_name = '$title',
p_painting_image = '$data'
p_painting_scale= '$scale'");
}
}
elseif (isset($_GET['show']))
{
$id = intval($_GET['show']);
$query="SELECT p_painting_imagetype, p_painting_upload_time, p_painting_image FROM {$table} WHERE p_painting_id";
$result=mysql_query($query);
if (mysql_num_rows($result) == 0)
die('no image');
list($ext, $image_time, $data) = mysql_fetch_row($result);
$send_304 = false;
if (php_sapi_name() == 'apache') {
// if our web server is apache
// we get check HTTP
// If-Modified-Since header
// and do not send image
// if there is a cached version
$ar = apache_request_headers();
if (isset($ar['If-Modified-Since']) && // If-Modified-Since should exists
($ar['If-Modified-Since'] != '') && // not empty
(strtotime($ar['If-Modified-Since']) >= $image_time)) // and grater than
$send_304 = true; // image_time
}
if ($send_304)
{
// Sending 304 response to browser
// "Browser, your cached version of image is OK
// we're not sending anything new to you"
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $ts).' GMT', true, 304);
exit(); // bye-bye
}
// outputing Last-Modified header
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $image_time).' GMT',
true, 200);
// Set expiration time +1 year
// We do not have any photo re-uploading
// so, browser may cache this photo for quite a long time
header('Expires: '.gmdate('D, d M Y H:i:s', $image_time + 86400*365).' GMT',
true, 200);
// outputing HTTP headers
header('Content-Length: '.strlen($data));
header("Content-type: image/{$ext}");
// outputing image
echo $data;
exit();
}
?>