php file force download

Posted by droidus on Stack Overflow See other posts from Stack Overflow or by droidus
Published on 2012-03-30T18:02:22Z Indexed on 2012/07/05 21:16 UTC
Read the original article Hit count: 318

Filed under:
|

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;
 }
 ?>

© Stack Overflow or respective owner

Related posts about php

Related posts about download