I am trying to read a image file (.jpeg to be exact), and 'echo' it back to the page output, but have is display an image...
my index.php has an image link like this:
<img src='test.php?image=1234.jpeg' />
The PHP Manual has this example:
<?php // open the file in a binary mode $name = './img/ok.png'; $fp = fopen($name, 'rb'); // send the right headers header("Content-Type: image/png"); header("Content-Length: " . filesize($name)); // dump the picture and stop the script fpassthru($fp); exit; ?>
The important points is that you must send a Content-Type header. Also, you must be careful not include any extra white space (like newlines) in your file before or after the
<?php ... ?> tags.
As suggested in the comments, you can avoid the danger of extra white space at the end of your script by omitting the
<?php $name = './img/ok.png'; $fp = fopen($name, 'rb'); header("Content-Type: image/png"); header("Content-Length: " . filesize($name)); fpassthru($fp);
You still need to carefully avoid white space at the top of the script. One particularly tricky form of white space is a UTF-8 BOM. To avoid that, make sure to save your script as "ANSI" (Notepad) or "ASCII" or "UTF-8 without signature" (Emacs) or similar.