HELP - Display images from a database field

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
eddy22
Forum Newbie
Posts: 3
Joined: Mon Apr 10, 2006 7:59 pm

HELP - Display images from a database field

Post by eddy22 »

I seriously need help with this one or a link to an example (non oop please). I managed to create a basic application to store info and an image in a mysql database. I can display the image all by itself or the data on the form all by itself but I can't find any way to display both :(

Displaying the image on the page by itself is pretty straight forward by retrieving the fields and using the header

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;


Displaying info in text fields by itself is pretty straight forward. Now what I am trying to do is create a basic html page and the image in the database would be in an <img > tag on the html.

I am getting a lot of problems with the headers or img displaying as a bunch of garbled text.

Can anyone help me out or point me in the right direction. I would really appreciate it.

Thanks.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: HELP - Display images from a database field

Post by timvw »

eddy22 wrote: header("Content-Disposition: attachment; filename=$name");
Why this header? What do you expect from it?
eddy22 wrote: Displaying info in text fields by itself is pretty straight forward. Now what I am trying to do is create a basic html page and the image in the database would be in an <img > tag on the html.
You can't simply add the image-data in an <img> tag. An <img> tag accepts an URL that is an image.
So you would have tag like <img src="myscript.php?img=123"> and then write myscript.php that outputs the image-data (and the correct content-type header.)
eddy22
Forum Newbie
Posts: 3
Joined: Mon Apr 10, 2006 7:59 pm

Post by eddy22 »

So then the correct header for the page content would be text/html? This is what I'm having trouble with. I have the code to display the image and that is why the header is what I have above. What I want is both the image and the html in a page. I haven't found an example of it anywhere yet. That is where I am stuck :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Images and HTML are sent in separate streams, not the same one. Therefore you send the HTML which will contain <img> tag references to the images. The source attribute is set to a script that identifies which image to pull for use in that reference. Each image requires a separate request.
eddy22
Forum Newbie
Posts: 3
Joined: Mon Apr 10, 2006 7:59 pm

Post by eddy22 »

I got it. FINALLY :)
I had to have 2 scripts to do this correctly. That is what I wasn't understanding. I had one script just to show the image and the other was the layout in HTML that called that script passing it the correct image ID to display.

Thanks.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Well, technically you can place them in one script too... But it seems easier to make two separate scripts...

Code: Select all

if (isset($_GET['image_id'])) {
  // this variable only exists when an image is requested, so display the image
  header('Content-Type: image/jpg');
  // do stuff to lookup and output image
} else {
  // display the image tag
  header('Content-Type: text/html');
  echo '<img src="?image_id=something" alt="..."/>';
}
Post Reply