As frymaster has already pointed out, it's probably not the best way to do it, but possible none the less.
Your first problem is that the script times out, and the downloads time out with it. That's easiliy fixed using
set_time_limit(time_in_seconds);
or something similar. Setting it to 0 would disable timeouts completely, but that would probably lead to crashiness.
Here's a take on a download-script. It even handles resumes. Not all my doing, but I can't for the life of me remember where some of the code came from in the first place. Note that it needs php running as a module under apache since it uses getallheaders().
/ simple download-script, with a resume-twist /
set_time_limit(3600); // the script times out after 3600 seconds (one hour)
if(!is_file($file) || !is_readable($file)) die("something went wrong"); // very basic check if the $file is valid
// look at the filename and try to work out a content-type
if(preg_match("/.(sfv|txt|nfo)$/i", $filename)) $contenttype = "text/plain";
elseif(preg_match("/.(avi)$/i", $filename)) $contenttype = "video/x-msvideo";
elseif(preg_match("/.(mp3)$/i", $filename)) $contenttype = "audio/mpeg";
else $contenttype = "application/octet-stream";
$fsize = filesize($file);
$headers = getAllHeaders();
if(isset($headers["Range"])) { // resume requested
header("HTTP/1.1 206 Partial content");
$val = split("=", $headers["Range"]);
if(ereg("-", $val[1])) {
$slen = ereg_replace("-", "", $val[1]);
$sfrom = $fsize - $slen;
header("Content-Length: " . $slen);
}
else if(ereg("-$", $val[1])) {
$sfrom = ereg_replace("-", "", $val[1]);
$slen = $fsize - $sfrom;
header("Content-Length: " . (string)((int)$fsize-(int)$sfrom));
}
$br = $sfrom . "-" . (string)($fsize-1) . "/" . $fsize;
header("Content-Range: bytes $br");
header("Content-Type: $contenttype");
header("Content-Disposition: attachment; filename=$filename");
header("Connection: close");
$fd = fopen($file, "rb");
fseek($fd, $sfrom);
fpassthru($fd);
exit;
}
else { // normal download
header("Content-Length: $fsize");
header("Content-Type: $contenttype");
header("Content-Disposition: attachment; filename=$filename");
header("Connection: close");
readfile($file);
exit;
}
/ simple download-script, with a resume-twist /
(My thanks to whoever wrote the resume-part in the first place. I feel awful not beeing able to give credit where it's due.)