Jump to content


Force Downloads

  • Please log in to reply
No replies to this topic

#1 ober

  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 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

Info: PHP Manual

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users