Force download of large files on a shared server
I am trying to add a download link to a large video file (approx 300MB) on someone's site but unfortunately they're on shared hosting (i've told them they will have to upgrade if they get many people downloading it). I don't want people to have to 'Save Target As' and I usually use this code to force downloads:
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/octet-stream开发者_运维问答");
header("Content-Disposition: attachment; filename={$file}");
header("Content-Transfer-Encoding: binary");
readfile($file);
This works fine with smaller files but not with larger ones and even after turning errors on I get no errors and no error log. I'm sure this is to do with the shared memory limit (or possibly a timeout) but does anyone know how I go about forcing downloads of large files on shared servers, ideally without javascript as i'm sure i won't be able to set the memory limits to be high enough?
Thanks very much,
Dave
The usual solution is to do the file output yourself and let the web server's own buffers handle things:
$fh = fopen($file, 'rb') or die("Unable to open $file");
while($data = fread($fh, 10240)) { // 10kbyte chunks.
echo $data;
}
fclose($fh);
精彩评论