最新消息: 新版网站上线了!!!

PHP实现断点继传

/*
* @url: 文件地址 
* @filename: 要保存的文件名 
*/ 
function _download($url, $filename) 
{ 
// 获得文件大小, 防止超过2G的文件, 用sprintf来读 
$filesize = sprintf("%u", filesize($url));
if (!$filesize) 
{ 
return; 
}
header("Content-type:application/x-msdownload\n"); //application/octet-stream 
header("Content-type:unknown/unknown;"); 
header("Content-disposition: inline; filename=\"".$filename."\""); 
header('Content-transfer-encoding: binary');
if ($range = getenv('HTTP_RANGE')) // 当有偏移量的时候,采用206的断点续传头 
{ 
$range = explode('=', $range); 
$range = $range[1];
header("HTTP/1.1 206 Partial Content"); 
header("Date: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($url))." GMT"); 
header("Accept-Ranges: bytes"); 
header("Content-Length:".($filesize - $range)); 
header("Content-Range: bytes ".$range.($filesize-1)."/".$filesize); 
header("Connection: close"."\n\n"); 
} 
else 
{ 
header("Content-Length:".$filesize."\n\n"); 
$range = 0; 
}
$fp = fopen($url, 'rb'); 
fseek($fp, $range); 
while ($bbsf = fread($fp, 4096)) 
{ 
echo $bbsf; 
} 
fclose($fp);
}

转载请注明:谷谷点程序 » PHP实现断点继传