Images in php

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
raygo22
Forum Newbie
Posts: 5
Joined: Sun Oct 05, 2008 1:48 pm

Images in php

Post 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!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Images in php

Post 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
raygo22
Forum Newbie
Posts: 5
Joined: Sun Oct 05, 2008 1:48 pm

Re: Images in php

Post by raygo22 »

lol! i am new at this!! can you explain how can i do all that?! thanks!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Images in php

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Images in php

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Images in php

Post by aceconcepts »

If you fetch your db data beforehand then that would be more ideal :D
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Images in php

Post by onion2k »

aceconcepts wrote:If you fetch your db data beforehand then that would be more ideal :D
How would you do that for an image stored in a database?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Images in php

Post by aceconcepts »

Before I explain anything lets see if we're all on the same page.

How is the images stored in the database?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Images in php

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Images in php

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Images in php

Post 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. :twisted:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Images in php

Post 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
Post Reply