homepage Welcome to WebmasterWorld Guest from 54.211.230.186
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Uploading images and relevant details to database
nil111

5+ Year Member



 
Msg#: 3715390 posted 6:31 am on Aug 5, 2008 (gmt 0)

Please ignore the facts that I haven't assigned any values to p_painting_tags.

<?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();
}
?>

 

dreamcatcher

WebmasterWorld Senior Member dreamcatcher us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3715390 posted 6:58 am on Aug 5, 2008 (gmt 0)

Hi nil111,

You haven`t given us much information. Just saying there is something wrong with your code isn`t helpful. Are you seeing errors? What is supposed to be happening that isn`t? Set your error reporting to E_ALL for debugging. Check your server error logs too.

dc

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved