Jump to content


Member Since 13 Jul 2004
Offline Last Active Jan 13 2014 06:52 PM

#43621 Force Downloads

Posted by ober on 09 June 2006 - 01:49 PM

Q.  How can I force download of files?
A.  I found this snip of code and it looks promising. 

// force to download a file
// ex, ( [url=http://localhost/php/download.php?file=C:/Apache]http://localhost/php/download.php?file=C:/Apache[/url] Group/Apache2/hongkong.php )
// hope this can save your time :-)

$file = $_REQUEST['file'];

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");


This hasn't been much of a common question, but it is helpful nontheless.


Just above the @readfile, addition of the following code will inform the browser of what's coming -- allowing it properly report download progress and estimated completion time.

    header('Accept-Ranges: bytes');
    header('Content-Length: ' . filesize($file));

Acknowledgement: steveo31, michaellunsford