I have made a download script to download movies from wmv-format. I also implemented a resume function.
Everything works fine only the download of a movie (+/- 500 M😎 stops around 250MB. I have to start all over again with downloading and never can download the complete movie.
Can somebody give me some advice what is causing this? Is there some filesize limitation by downloading with PHP? What about the headers?
Here is the script:
function dl_resume($path) {
//First, see if the file exists
if (!is_file($path)) { die("<b>404 File not found!</b>"); }
//Gather relevent info about file
$filename = basename($path);
$size=filesize($path);
if (isset($_SERVER['HTTP_RANGE'])) {
// Support for partial transfers enabled and browser requested a partial transfer
header("HTTP/1.1 206 Partial content\n");
$start = preg_replace(array("/(\040*|)bytes(\040*|)=(\040*|)/","/(\040*|)\-.*$/"),array("",""),$_SERVER['HTTP_RANGE']);
if ($size < $start)
{
header("HTTP/1.1 411 Length Required\n");
echo "Trying to download past the end of the file. You have probably requested the wrong file. Please try again.";
}
$transfer_size = $size - $start;
header("Accept-Ranges: bytes");
header("Content-Range: bytes ".$transfer_size."-".($size-1)."/".$size);
header("Content-Length:".$transfer_size."\n");
}
else{
header("HTTP/1.1 200 OK\n");
header("Content-Range: bytes 0-".($size-1)."/$size");
header("Content-Length: $size");
}
header("Cache-control: private\n"); // fix for IE to correctly download files
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache\n"); // fix for http/1.0
header("Content-Description: File Transfer");
header("Content-Type: video/x-ms-wmv");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Transfer-Encoding: binary");
//open the file
$fh = fopen( $path, 'rb' );
//Set file pointer to the wright location
if (isset($transfer_size)){
fseek($fh,$transfer_size);
}
if ($speed_limit != 0) {
$chunk = $speed_limit * 1024;
// Stream our file in chunks
while(!feof($fh) and (connection_status()==0)) {
echo fread($fh, $chunk);
flush();
sleep(1);
}
}
else {
// File streaming for the impatient
while(!feof($fh)) {
echo fread($fh, 4096);
}
}
fclose($fh);
return((connection_status()==0) and !connection_aborted());
}