Jump to content

Archived

This topic is now archived and is closed to further replies.

ober

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

Share this post


Link to post
Share on other sites

×

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.