By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,656 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Getting error when outputting image using headers. (Solution)

n8kindt
100+
P: 221
i just spent nearly 3hrs trying to debug this problem so i figured i would post this just in case someone else has the same problem in the future. if you're trying to output binary data as an image in php and getting an error message like this: "the image [ ] could not be displayed because it contains errors" or a broken image red box looking thingie then check this out.

this code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header('Content-type: image/jpeg');
  3. include $_SERVER['DOCUMENT_ROOT'].'/includes/globals.php';
  4. $db_con = open_db();
  5.  
  6.         $image_q = 'SELECT `image` FROM `fc_pix`';
  7.         $image_r = mysql_query($image_q);
  8.         while ($image_w = mysql_fetch_array($image_r)){
  9.         echo $image_w['image'];
  10.         }
  11.     close_db($db_con)
  12. ?>
  13.  
IS NOT THE SAME AS:

Expand|Select|Wrap|Line Numbers
  1.  
  2. (*********pretend this is a blank line***********)
  3. <?php
  4. include $_SERVER['DOCUMENT_ROOT'].'/includes/globals.php
  5. header('Content-type: image/jpeg');
  6. $db_con = open_db();
  7.  
  8.         $image_q = 'SELECT `image` FROM `fc_pix`';
  9.         $image_r = mysql_query($image_q);
  10.         while ($image_w = mysql_fetch_array($image_r)){
  11.         echo $image_w['image'];
  12.         }
  13.     close_db($db_con)
  14. ?>
  15.  
that blank line at the top will throw the image parsing off completely and even though by all appearances all the data is being output correctly, that extra line gap will not allow it to be read. just thought i'd post this b/c when i searched google i found a lot of people had this problem and ultimately never figured it out.
Sep 17 '08 #1
Share this Question
Share on Google+
1 Reply


Markus
Expert 5K+
P: 6,050
FYI: That is because when you use header()s, there can be no output whatsoever before the headers are sent. You'll receive an 'headers already sent' error.

Cheers.

PS: Please provide more meaningful post names when posting. I have changed yours for you.

Moderator
Sep 17 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.