0

I am using PHP and apache, for some reason Images are refusing to be shown!

PDFs are fine, there are no errors.

Please can someone help!

The code is here:

    header('Content-Type: image/png');
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Content-Length: ".filesize('image.png'));
    print file_get_contents('image.png');

For some reasons this image doesn't show, but a PDF will.

both files have exactly the same file permissions.

Can anyone help?

Thanks

Andy

  • Try changing `print file_get_contents('image.png');` to `readfile('image.png');` – user1477388 Sep 30 '13 at 16:55
  • Nope, it can read the file because I can print it out and get the gibberish. Thanks though – Andy Pegg Sep 30 '13 at 16:57
  • You're welcome. I was just reading http://stackoverflow.com/questions/1851849/output-an-image-in-php – user1477388 Sep 30 '13 at 17:00
  • Look at the headers in your browser's request inspector and make sure the content-type header isn't being overridden by your framework and/or server. – ceejayoz Sep 30 '13 at 17:05
  • it works right away - you must have a problem with the path to image.png – davidkonrad Sep 30 '13 at 17:22
  • **Remember** : You must not echo / print ANYTHING out before the piece of code above, not even a single blank or empty line before - then you will get "gibberish" :) – davidkonrad Sep 30 '13 at 17:25
  • I would recommend using the `readfile` function instead of `print file_get_contents`, as that will use less memory. (See http://stackoverflow.com/a/909699/1721527) Also, try adding `exit()` right after it to make sure nothing else is outputted. You could also try running `ob_clean();flush();` before outputting the image to make sure nothing else will be outputted. – Joshua Dwire Sep 30 '13 at 17:32
  • Thank you Joshua Dwire! ob_clean() worked, still not quite sure where an echo is coming from, prior to adding this, I put an exit() after EVERY print/echo and it didn't exit anywhere! But it works, so I don't really mind. Just out of curiosity, does anyone know why it worked with a PDF but not a PNG? Thanks all for your help, much appreciated. – Andy Pegg Oct 01 '13 at 07:47

0 Answers0