Forum Moderators: coopster
$fileToOpen = "/home/pathtofile/" . $result['id'] . "." . $result['filetype'];
$handle = fopen($fileToOpen, "rb");
$contents = fread($handle, filesize($fileToOpen));
fclose($handle);
header("Pragma: public");
header("Expires: 0");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0', false);
header("Content-length: " . $result['filesize']);
header("Content-Disposition: attachment; filename=\"" . $result['filename'] . "\"");
header('Content-Transfer-Encoding: binary');
print $contents;
Yes, I realise that I'm missing the Content-Type header, but that's for a reason. Some of the files being downloaded don't even seem to have a mime type associated with them (such as the dvf file I mentioned before). I don't really care if my browser thinks the file is a htm file, just naming it correctly will be sufficient. And no, putting that header in doesn't seem to make any different (though I must admit, I have no idea what to set the content type to for dvf files).
The user retrieves the file through a normal anchor tag. So, it'll be something like
<a href="?file=38">Download</a>
and then at the top of the page, i've got
if (isset($_GET['file'])) {
//do stuff that i mentioned before
}