Page 1 of 1

Output image from a mysql to a html page

Posted: Thu Sep 18, 2003 7:04 pm
by Bonta_SWE
I cant find how to output an image that are storing in a mysql database to a html page..
Can some one help me please

I can insert a image into an mysql just fine, but getting it out,, i cant understand..

Its like this, my members on my site should upload an picture on what ever they want, and it should be posted on the userinformation.


//Thanx

Posted: Thu Sep 18, 2003 9:29 pm
by fractalvibes
Write a script that is responsible for streaming the image to the browser. It needs to:
1.) read the database for that image
2.) determine the content-type (determine and store in db when you
load the image)
3.) Add a http header setting the content type appropriately
4.) echo the image out to the browser

Other pages will call this script via a link tag as in:

<a href=DisplayImage.php?ID=437>Cat Photo</a>

Check the PHP manual for references to AddHeader and also
for the various properties available to you when you upload a file.
Be sure and also store the content type - image/gif, image/pjpeg, even application/pdf! That way you have a universal(almost) media server application.

fv

Posted: Thu Sep 18, 2003 9:52 pm
by Cruzado_Mainfrm
the script to read the image is like this:

Code: Select all

$id = $_GET['id'];
@mysql_connect("localhost","root");
@mysql_select_db("test");
$query = "SELECT binarydata FROM images WHERE id = '$id'";
$result = @MYSQL_QUERY($query);
$data = @mysql_result($result,0,"binarydata");
Header( "Content-type: image/jpeg"); //image/png also you can even say application/x-shockwave-flash if you have the binary data of a .swf flash file 
echo $data;
hope this helps
call it like this: image.php?id=1
be sure to edit this to fit your needs of course

Posted: Fri Sep 19, 2003 4:19 am
by Bonta_SWE
Hi,

Cruzado_Mainfrm i tried your code and i dont get it to work wright.
My mysql table look like this

TABLE NAME image
image_id int(10) unsigned NOT NULL auto_increment,
image_type varchar(50) NOT NULL default '',
image longblob NOT NULL,
image_size bigint(20) NOT NULL default '0',
image_name varchar(255) NOT NULL default '',
image_date datetime NOT NULL default '0000-00-00 00:00:00',
UNIQUE KEY image_id (image_id)

I dont get it how i should print the image, your code looks to work, but i get a "red cros" (cant find the image) like this
http://www.theemperorsfinest.com/test/image.php