header("Content-type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
if ($fp = fopen("$file", 'rb')) {
while(!feof($fp) and (connection_status()==0)) {
print(fread($fp, 1024*7));
flush();
}
$status = (connection_status()==0);
fclose($fp);
}
This has always worked out well for me.
[edit] It also has a bit in there to limit the amount of bandwidth used by a given user, as this script ran over 100mb connections, we had a tendency to saturate the data center's connection 🙂 To fix it just play with the print(fread($fp.... part, as it handles the buffering.