Re: DB-stored images
| From: | Marcus Fleige | Date: | Tue, 26 Nov 2002 10:30:41 +0000 |
| Subject: | Re: DB-stored images | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-23664@lists.php.net to get a copy of this message | ||
hi,
on Tuesday, November 26, 2002, 10:31:58 AM, you wrote:
OS> Hi!
OS> I have a db where we stores images in the database but I haven't figured out how I
OS> display
OS> that image after I've selected it from the table.
OS> I've tried with <IMG SRC='$data'> and <IMG SRC=$data> but $data
OS> just return the Select-query:
OS> <html><head><title></title></head>
OS> <body bgcolor=cccccc text=000000 link=0000ff vlink=0000ff>
OS> <B>Picture</B><BR><UL>
OS> <IMG SRC='SELECT PICTURE FROM KATALOGUE WHERE ISBN =
OS> '91-29-65480-7''>
OS> </UL></body></html>
OS> Anyone have an idea what I can do?
you can't just insert the result of the database-query.
when you store an image as blob-type in the db, the mime type
isn't saved with the image-stream. so, when you set your imgsrc-tag,
the webserver does not know what type of data comes next.
you need an additional script to retrieve the image-data from the db
and set the mime type.
here's a solution i got running on my server (for jpgs):
save a picture to the db:
<?php
$link = mysql_connect(localhost,"user","pass");
mysql_select_db("database") or die mysql_error();
$handle = fopen($filename,"r");
while (!feof($handle))
{
$pic=$pic.fread($handle, 1000);
}
fclose($handle);
$pic = mysql_escape_string ($pic);
mysql_query("insert into pics (pic,header) values
('$pic','image/jpeg')");
?>
script to return image (named: getpic($id)):
<?php
$link = mysql_connect(localhost,"user","pass");
mysql_select_db("database") or die mysql_error();
$result = mysql_query("select blob,header from pics where picid=$id");
$data = mysql_fetch_array($result);
header ("Content-type: $data[header]");
print($data[$pic]);
?>
you can call the script from php by doing the following:
<?
echo "<img src=\"".getpic($id)."\">;
?>
hope this helped! ;-)
greetings from germany,
marcus
--
^V^
Marcus Fleige
marcus.fleige@gmx.de