Jump to content


ober

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

#43621 Force Downloads

Posted by ober on 09 June 2006 - 08:49 AM

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

// 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");
@readfile($file);

?>

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

MODIFICATION 6/23/06

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


Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com