phpfile-get-contentsreadfilefile-existspublic-html

Retrieving image file from relative folder using readfile() or file_get_contents() not working


My root folder is public_html. Outside of public_html I have a folder called images. In order for me to retrieve images from this folder I query the database for correct image name & extension in a file that is stored inside public_html folder -> forms folder -> check_images.php

My database output is:

$output['filetype'] -> jpg
$output['name'] -> 1

The variable $file is the relative path + the filename + filetype, which makes '../../images/1.jpg'.

Now for some reason fetching the image doesn't work, however file_exists() is working fine.

The header is:

header('Content-type: image/jpeg');

What am I doing wrong? and why isn't readfile() or file_get_contents() working as it should when they are exactly the same folder as file_exists()

Entire code can be found here: http://pastebin.com/MhkAfY4s

What is currently being displayed is an empty image / or a broken image when being linked to.

UPDATE

  1. First I check the URL: http://pastebin.com/Fz36GmVD
  2. Then I run the image code: http://pastebin.com/fZSRuJ4r

Solution

  • What is happening is most likely that there is other output than the image, thus messing up the image binary data. Have a look at the source of the page, beware of any spaces before your <?php as well as any after the ending ?>one (which you should omit, as it is not needed)

    To further help with large file downloads I suggest you use XSendFile to handle the heavy lifting for you.

    Update:

    To sum it up, the most likely reason to why it didnt work is the ob_start with gzhandler, simply removing it did not do anything, that probably because of the E-Tag and browser still showed cached content.