-2
$sql="SELECT * FROM  `blob` ";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)) {
    $image=$row ['image'];
    header("content-type: image/jpg");
    echo '<img src="path/'.$image.'" width="360" height="150">';
}

This is the code i'm using to display the image uploaded to my database... however, i get the error message that the headers have already been sent... i have no idea what i'm doing wrong!

i can see on my website where my images are meant to be but no actual image!

kero
  • 10,647
  • 5
  • 41
  • 51
  • 3
    Don't return image headers **and** an HTML `` tag. You'll need one script to return the image data, and a second one to return the HTML pointing to it. – Blazemonger Jan 14 '14 at 21:14
  • If the image is stored as a blob in the db, you don't need to send the `img` tags – Ayush Jan 14 '14 at 21:14
  • 1
    You mustn't use the `content-type` header here: it is sent along with the resource itself, but not in the document that embeds the resource – Pekka Jan 14 '14 at 21:14
  • Are you sending an image or are you sending HTML? Your code is trying to do both, but the two are mutually exclusive. – David Jan 14 '14 at 21:16
  • @David Not entirely true! One could embed the image in the HTML using a [data URL](http://stackoverflow.com/questions/9646052/pulling-blob-image-data-from-mysql-in-php), although that's only practical for small images (and clearly not what's being attempted here). – Blazemonger Jan 14 '14 at 21:17
  • @Blazemonger: Which wouldn't call for the use of this header. The code is still confused between the two entirely separate responses. – David Jan 14 '14 at 21:17
  • @user3195772 If possible, re-think your strategy: It would make a lot more sense (and your life easier...) to store images in the file-system and paths to these images in the database. – jeroen Jan 14 '14 at 21:22
  • Sorry i'm new to php... i'm trying to retrieve the jpg image from my database (upload process and retrieve on the same page) i can see where the image is meant to be displayed (small paper in a box icon) but the actual image wont display! Totally lost i've tried so many different approaches! (p.s he/him is a she/her) – user3195772 Jan 14 '14 at 21:33

3 Answers3

0

In general: When you get this message it seems that there is an output done before. It can even be a simple "whitespace" character like [:space:].

But: In addition to that you seem to get some things wrong. You do create HTML (!) but you send a header-message that tells the browser that it get's binary data of an image.

So when you really have binary data in your database you have to output binary data and not any html "thingy". I really really recommend you to read a good book about how a server works and what header(..) actually does.

Please also have a look here:

PS: I did not get into any more detail about your question neither provide a solution because I do not think this would help you rather than solving this issue of misunderstanding temporarily.

Community
  • 1
  • 1
androidavid
  • 1,258
  • 1
  • 11
  • 20
0

Your code is totally wrong; storing images inside a MySQL database is a bad idea and you're using your image data as a path to a nonexistent image; if you still want to stick with the idea of having images in your database, here's example code that would work :

while ($row = mysql_fetch_array($query)) {
    echo '<img src="data:image/jpeg;base64,'.base64_encode($row["image"]).'" />';
}

This encodes the image into base64 and then uses that as a data URI inside an img; no headers needed.

Also, mysql is deprecated; use mysqli or PDO instead.

-2

The problem is that you're doing it in a while loop, which will be executed multiple times:

while($row=mysql_fetch_array($query)) {
    $image=$row ['image'];
    header("content-type: image/jpg"); // Headers are sent here
    echo '<img src="path/'.$image.'" width="360" height="150">'; // Content here
}

When mixing headers like this, what you would normally want to do is put the header before any content. However in your case, this wouldn't make sense either because you are sending "content-type: image/jpg". If you are echoing an image to the browser using HTML, the content-type should be text/html (which is sent by PHP by default). So just leave that line out completely. If you are trying to send the actual binary contents of an image, then you wouldn't use an HTML tag, just send the Content-Type header and then echo the contents for one single image.

Mike
  • 23,542
  • 14
  • 76
  • 87