Page 1 of 1
Images in php
Posted: Wed Oct 08, 2008 5:41 am
by raygo22
Hello. I want to store pictures in a database and then call them from php. I tried echo but it only shows a lot of weird characters. Can somebody help me? I gave to the picture the data type "longblob" is that correct? thanks!
Re: Images in php
Posted: Wed Oct 08, 2008 5:55 am
by josh
You have to output a content-type header telling the browser what mime type the data uses, otherwise apache will default it to plaintext
Re: Images in php
Posted: Wed Oct 08, 2008 6:04 am
by raygo22
lol! i am new at this!! can you explain how can i do all that?! thanks!
Re: Images in php
Posted: Wed Oct 08, 2008 8:16 am
by aceconcepts
When you output an image you typically use the <img> tag.
So, what you can do is get the image's path from your database and output like so:
Code: Select all
<img src="your/image/path/<? echo $image_name; ?>" />
Obviously the image name should include the file type e.g. jpg or gif etc...
Get it?
Re: Images in php
Posted: Wed Oct 08, 2008 9:07 am
by onion2k
Except in this case "your/image/path/<? echo $image_name; ?>" will be the path to a script that fetches the image content from the database.
Re: Images in php
Posted: Wed Oct 08, 2008 9:14 am
by aceconcepts
If you fetch your db data beforehand then that would be more ideal

Re: Images in php
Posted: Wed Oct 08, 2008 9:41 am
by onion2k
aceconcepts wrote:If you fetch your db data beforehand then that would be more ideal

How would you do that for an image stored in a database?
Re: Images in php
Posted: Wed Oct 08, 2008 9:46 am
by aceconcepts
Before I explain anything lets see if we're all on the same page.
How is the images stored in the database?
Re: Images in php
Posted: Wed Oct 08, 2008 9:48 am
by onion2k
aceconcepts wrote:How is the images stored in the database?
The original poster said they're in a LONGBLOB column, so lets assume they're in there as binary data.
Re: Images in php
Posted: Wed Oct 08, 2008 9:52 am
by aceconcepts
I see, I didn't realise the picture was being store and retrieved directly. I thought the image was being stored in an images dir and referenced by path.
Re: Images in php
Posted: Wed Oct 08, 2008 9:56 am
by onion2k
aceconcepts wrote:I see, I didn't realise the picture was being store and retrieved directly. I thought the image was being stored in an images dir and referenced by path.
He could be I suppose. LONGBLOB is a bit of a strange choice of data type for a column storing a filename if that's the case though.

Re: Images in php
Posted: Wed Oct 08, 2008 11:47 pm
by josh
You never know, he could be storing a serialized object too.. point is generalized questions with generalized background on the info get generalized answers