Image Header and output buffering or

I am using a file that retrieves image data from a database and displays it using an image header.

I need to insert the image into an html image tag and am getting defeated because the header fails since I have to send a bunch of html before inserting the image file.

I think I can use output buffering to achieve my goals here but am not sure just how to do that. There may also be another solution that I am not aware of.

The files involved are included below. The line of code where the first file using the header is inserted is in the second file below and is followed by a comment like this // ************* to identify the line of code.

Any help would be greatly appreciated.



The file that retrieves and displays the image is here:

<?php  // get_image.php

// ::::::::::::::::::::::::::::::::::::::::::::::::::::://
//                                                      //
//   Retrieve image data from database and display     //
//                                                      //
// ::::::::::::::::::::::::::::::::::::::::::::::::::::://


$id = $_GET['id'];
$sql = "SELECT image FROM images WHERE id_image = '$id'";
$stmt = $db->query($sql); 
$row = $stmt->fetch(PDO::FETCH_ASSOC);

header("Content-type: image/jpeg");

echo "{$row['image']}";


I am using this file in the src attribute of an <img> tag to display it on the page. As mentioned above, it fails since it is attempting to send a header after other content has been sent.


//include ('includes/');
//include (DB);

$sql = "SELECT id_image, name, size, description FROM images";
$stmt = $db->query($sql); 

echo '<table width="100%">';

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

$id = $row['id_image'];

//echo 'In the right place.';

print <<<STUFF
    <td  width="220px"><img src="__functions/get_image.php?id=$id" width="200px" ></td> // *************
    <td><table cellpadding="0">
        <td valign="bottom">ID: $id</td>
        <td valign="bottom">Name: {$row['name']}</td>
        <td valign="middle">Image Size: {$row['size']}</td>
        <td valign="top"><form><input type="text" value="<img src='__functions/get_image.php?id=$id&column=image' >" size='60' readonly="true" /></form></td>


<img src=“__functions/get_image.php?id=$id” width=“200px” >

Shouldn’t this be a fully qualified web url ?

It is the correct path to the file and the file is invoked. The issue is not that the script is not running, it is running but the header issue is preventing it from displaying the image.



Very first line, on top of your script:


At the very bottom of your scipt:


I don’t understand. That just doesn’t sound right.

This is how it should be: You have one file, HTML (with PHP in your case), and you have another, an image file (a PHP generated one in your case). So that’s two separate PHP files. The image PHP file, in its entirity, amount to an image – absolutely no HTML involved in that. The HTML one is standard stuff. Two separate PHP files on the server. One generating HTML, the other an image. So " the header fails since I have to send a bunch of html before inserting the image file." doesn’t stand up to logic / how it should be.

While you’re sorting this out you can just forget the PHP HTHML file. Just concentrate on getting the image file to work – access it directly by its address in browser bar. No image?; then that’s where the problem is. Nothing to do with the other PHP HTML file.

You need to find out what echo "{$row[‘image’]}"; actually is outputting – what bytes. Post it here – just the start and end if it’s really big.

And to do this, shouldn’t you be using the GD library?
Or at least something similar? Maybe that’s not necessary? I’m not sure. Thought it was.

Just try accessing the image file directly in your browser and see what’s output.

Johnyboy & others – Actually, there is something else going on here that I need to investigate. Thanks for the help so far. I will get back to you later today with a continued question or a report.