0

I'm trying to display an image using a PHP script. Basically so the php script is passed on the full path to the image, and it then displays that image in the browser. I've checked to make sure the image exists, it is being read correctly, etc, however in the browser i just see the broken image box (e.g the small red cross in IE) if I go there.

My script sends out these headers:

<?php
header('Last-Modified: ' . gmdate('D, d M Y H:i:s T', filemtime($file)));
header('Content-Type: '.$mime);
header('Content-Length: '.filesize($file)."\n\n");
header('Etag: '.md5($file));
echo $file;
die;

$file contains something like '/var/www/htdocs/images/file.jpg' which works. the $mime is 'image/jpeg'.

I have also tried echoing file_get_contents($file) but it didn't work either.

What is the problem, any thoughts?

3
  • 1
    Cause this is a bad question. Commented Mar 19, 2009 at 14:45
  • 1
    Extra space or content before <? can generate bad output. Usually you must verify if your file UTF8 with no bOM! Open on notepad and delete that extra space Commented Nov 28, 2014 at 11:25
  • Thanks, Miguel. I wasn't able to get images to load from readfile due to having content before <?. Commented Jan 21, 2016 at 14:34

3 Answers 3

4
<?php
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension ) 
    {
    case "gif": 
        $ctype="image/gif"; 
        break;
    case "png": 
        $ctype="image/png"; 
        break;
    case "jpeg":
    case "jpg": 
        $ctype="image/jpeg"; 
        break;
    default:
    }
ob_clean();  // add this before header
header('Content-type: ' . $ctype);
readFile($file);  
?>

Some time extra space may be produced in any other file.So those extra spaces can be removed by calling ob_clean() before header() is called.

Sign up to request clarification or add additional context in comments.

1 Comment

ob_clean(); This should be the accepted answer. Thank you
2

Striving for simplicity...

<?php
header('Content-type:' . mime_content_type($file));
readfile($file);

should function as expected.

2 Comments

doesn't work :( shows same broken image icon :( typing the url to the image directly in the browser works, and if i type echo readfile($file) and omit the header, i can see the whole lot of gibberish which makes up the image
No need to type echo readfile() as readfile() writes directly to the output buffer. Adding echo would only pollute the image by echoing the number of bytes returned.
2

I found the answer, i had some extra whitespace after the ?> tag which was causing the header to not work. grrrrrrr

2 Comments

I often did this mistake also.. haha
It's common for people not to include the end PHP tag except if there's HTML after it. It's also part of the Zend coding standards. It can create a lot of bugs concerning headers and output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.