saving and displaying picture in mysql

saving and displaying picture in mysql

am 04.12.2006 11:52:43 von Gaga

How to upload picture to local ( or remote ) mysql table field ?
Photos are under 1 MB and i need to put them into db and then pull them out
for displaying.

Thank you !

Re: saving and displaying picture in mysql

am 04.12.2006 12:37:50 von Sean

"Gaga" wrote in message
news:el0ukg$djf$1@ss408.t-com.hr...
> How to upload picture to local ( or remote ) mysql table field ?
> Photos are under 1 MB and i need to put them into db and then pull them
> out
> for displaying.
>
> Thank you !
>
>


PHP & MySQL Answer............

CREATE TABLE `ae_gallery` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(64) character set utf8 NOT NULL,
`ext` varchar(8) character set utf8 NOT NULL,
`image_time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
`data` mediumblob NOT NULL,
PRIMARY KEY (`id`)
);

You can use any name for this table, just change $table variable at the
begining of the image gallery code. Before using following example create
sql-table (execute CREATE TABLE query above) and change variables ($db_host,
$db_user, $db_pwd, $database, $table) to your MySQL / database settings.

$db_host = 'localhost'; // don't forget to change
$db_user = 'mysql-user';
$db_pwd = 'mysql-password';

$database = 'test';
$table = 'ae_gallery';
// use the same name as SQL table

$password = '123';
// simple upload restriction,
// to disallow uploading to everyone


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");

// This function makes usage of
// $_GET, $_POST, etc... variables
// completly safe in SQL queries
function sql_safe($s)
{
if (get_magic_quotes_gpc())
$s = stripslashes($s);

return mysql_real_escape_string($s);
}

// If user pressed submit in one of the forms
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// cleaning title field
$title = trim(sql_safe($_POST['title']));

if ($title == '') // if title is not set
$title = '(empty title)';// use (empty title) string

if ($_POST['password'] != $password) // cheking passwors
$msg = 'Error: wrong upload password';
else
{
if (isset($_FILES['photo']))
{
@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
{
$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 {$table}
SET ext='$ext', title='$title',
data='$data'");

$msg = 'Success: image uploaded';
}
}
elseif (isset($_GET['title'])) // isset(..title) needed
$msg = 'Error: file not loaded';// to make sure we've using
// upload form, not form
// for deletion


if (isset($_POST['del'])) // If used selected some photo to delete
{ // in 'uploaded images form';
$id = intval($_POST['del']);
mysql_query("DELETE FROM {$table} WHERE id=$id");
$msg = 'Photo deleted';
}
}
}
elseif (isset($_GET['show']))
{
$id = intval($_GET['show']);

$result = mysql_query("SELECT ext, UNIX_TIMESTAMP(image_time), data
FROM {$table}
WHERE id=$id LIMIT 1");

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

MySQL Blob Image Gallery Example


if (isset($msg)) // this is special section for
// outputing message
{
?>







}
?>

Blob image gallery


Uploaded images:





$result = mysql_query("SELECT id, image_time, title FROM {$table} ORDER BY
id DESC");
if (mysql_num_rows($result) == 0) // table is empty
echo '
  • No images loaded
';
else
{
echo '
    ';
    while(list($id, $image_time, $title) = mysql_fetch_row($result))
    {
    // outputing list
    echo "
  • ";
    echo " – ";
    echo "{$image_time}
  • ";
    }

    echo '
';

echo '
';
echo '

';

echo '';
}
?>


Upload new image:





























Regards,

Sean

Re: saving and displaying picture in mysql

am 04.12.2006 13:18:14 von Ulyx

Sean, thank you very much for your example !
I will try it today and then i get back to you ( + two tables question )

Regards !

Re: saving and displaying picture in mysql

am 06.12.2006 19:32:46 von Ulyx

I have small problem with understandign of your code.
When i first start your code i get error: mysql_num_rows(): supplied
argument is not a valid MySQL result resource in:

but i can make upload. After upload i look int db, but there is no data
:-((.

Re: saving and displaying picture in mysql

am 07.12.2006 11:06:49 von Sean

"Gaga" wrote in message
news:el72bh$lfa$1@ss408.t-com.hr...
>I have small problem with understandign of your code.
> When i first start your code i get error: mysql_num_rows(): supplied
> argument is not a valid MySQL result resource in:
>
> but i can make upload. After upload i look int db, but there is no data
> :-((.
>
>
>
>

Have you used the same table as per my example or tried to adapt to your own
tables?

Sean

Re: saving and displaying picture in mysql

am 12.12.2006 17:48:25 von Ulyx

When i pass sql data i get error "sql error #1064.... for the right syntax
to use, near current_timestamp on update...
So i have entered data manualy ant all works fine accept there is no upload
into db :-(




"Sean" wrote in message
news:1165482593.18855@kestrel.skynet.co.uk...
>
> "Gaga" wrote in message
> news:el72bh$lfa$1@ss408.t-com.hr...
> >I have small problem with understandign of your code.
> > When i first start your code i get error: mysql_num_rows(): supplied
> > argument is not a valid MySQL result resource in:
> >
> > but i can make upload. After upload i look int db, but there is no data
> > :-((.
> >
> >
> >
> >
>
> Have you used the same table as per my example or tried to adapt to your
own
> tables?
>
> Sean
>
>
>