Page 1 of 1

HELP - Display images from a database field

Posted: Mon Apr 10, 2006 8:05 pm
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.

Re: HELP - Display images from a database field

Posted: Mon Apr 10, 2006 9:32 pm
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.)

Posted: Tue Apr 11, 2006 8:06 am
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 :(

Posted: Tue Apr 11, 2006 8:09 am
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.

Posted: Tue Apr 11, 2006 9:17 am
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.

Posted: Tue Apr 11, 2006 12:04 pm
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="..."/>';
}