4

When I use this code to download this image (only used for testing purposes), I open the downloaded image, and all it gives me is an error. i tried it in chrome. opening it with windows photo viewer, it says that it can't display the picture because it is empty??? here is the code:

<?PHP
 // Define the path to file
 $file = 'http://www.media.lonelyplanet.com/lpi/12553/12553-11/469x264.jpg';

 if(!file)
 {
     // File doesn't exist, output error
     die('file not found');
 }
 else
 {
     header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
 }
 ?>
5
  • can readfile read a remote file?? Commented Mar 30, 2012 at 18:04
  • Try removing the headers and see if you get an error Commented Mar 30, 2012 at 18:08
  • when i try downloading an image from my site, i get the same error. and let me try removing the header. Commented Mar 30, 2012 at 18:11
  • so i removed the headers, and got a lot of random ascii characters Commented Mar 30, 2012 at 18:13
  • just notice that the above script has another obvious problem: if(!file) does not check whether the file exists or not, but checking the string exists instead. Commented Nov 9, 2016 at 3:25

4 Answers 4

4

I've had a chance to work it out. Your problem is two-fold.

First, remove the www. from the url.

Second, remove the call to filesize($file) which is throwing an error because PHP doesn't know the size of the file before it downloads the file. (really, just remove the whole line)

Removing these two things, I was successful.

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

1 Comment

Yes, filesize should be used only when providing download option of local files (files at server).
2

Replace ob_clean() with ob_end_clean()

You're still buffering, so none of the image contents get to the browser.

2 Comments

wait, I don't see you starting a buffer... Why is that ob_clean there anyway?
have you tried setting the mimetype correctly to image/jpeg?
1

If your intention is just the download the file from a third party on click of a link, you could use the new property download in the anchor tag.

The code will look something like

<a download href="path/to/the/download/file"> Clicking on this link will force download the file</a>

It works on firefox and chrome latest version. Should I mention that I didn't check it in IE? :P

Comments

1

Replace:

ob_clean();
flush();
readfile($file);

With:

echo file_get_contents($file);

8 Comments

it was valid when i was using it. image: media.lonelyplanet.com/lpi/12553/12553-11/469x264.jpg
still didn't work, by the way, using header('Content-Type: image/jpeg');
Ha. Get rid of the www. in your url string
so it should be like: media.lonelyplanet.com/lpi/12553/12553-11/469x264.jpg? still doesn't work :p
|

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.