Getting error when outputting image using headers. (Solution)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • n8kindt
    New Member
    • Mar 2008
    • 221

    Getting error when outputting image using headers. (Solution)

    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:
    Code:
    <?php
    header('Content-type: image/jpeg');
    include $_SERVER['DOCUMENT_ROOT'].'/includes/globals.php';
    $db_con = open_db();
    
            $image_q = 'SELECT `image` FROM `fc_pix`';
            $image_r = mysql_query($image_q);
            while ($image_w = mysql_fetch_array($image_r)){
            echo $image_w['image'];
            }
        close_db($db_con)
    ?>
    IS NOT THE SAME AS:

    Code:
     
    (*********pretend this is a blank line***********)
    <?php
    include $_SERVER['DOCUMENT_ROOT'].'/includes/globals.php
    header('Content-type: image/jpeg');
    $db_con = open_db();
    
            $image_q = 'SELECT `image` FROM `fc_pix`';
            $image_r = mysql_query($image_q);
            while ($image_w = mysql_fetch_array($image_r)){
            echo $image_w['image'];
            }
        close_db($db_con)
    ?>
    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.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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

    Comment

    Working...