Jump to content

Force Downloads


Recommended Posts

[b]Q.  How can I force download of files?[/b]
A.  I found this snip of code and it looks promising. 
[code]<?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);

?>[/code]

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

[b]MODIFICATION 6/23/06[/b]

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.

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

Acknowledgement: steveo31, michaellunsford
Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.