how to display images from the database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

how to display images from the database

Post by davidklonski »

Hi

I have read the article in CodeWalkers about how to store and retrieve images from the database.

The article explains that once I have the type of the image and the image itself, then I can display them like this:

Code: Select all

Header ("Content-type: $image_type");
print $image;
I would like to be able to display the image within HTML content. Meaning that instead of writing:
...<img src="xxx.jpg"></img>...
I would display that image in that place. Note that the <img> tage may be embedded inside a much more complex html code.

does anyone know how I can make that last move?

thanks in advance
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Grab the image from the database and store it as a tmp file, and then just include it with <img>, or you could save the image as a file on your server instead of in the database - and just hold the location in the database.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could also do <img src="genimage.php?id=4"> where genimage.php would use $_GET['id'] to grab the image from the db and output the appropriate header (as you have above). But storing the path/image name in the db is a better option imho. Better to keep binary data on the file system.
Post Reply