Home >php教程 >php手册 >php 断点续传下载功能

php 断点续传下载功能

WBOY
WBOYOriginal
2016-06-13 10:09:511198browse

我们来看看用php 断点续传吧,这个功能很好的节省时间

 

我们来看看用php 断点续传吧,这个功能很好的节省时间

header("Cache-Control: public");
header("Accept-Ranges: bytes");

$file = "a.rar";
$filename = "a.rar";

$size=filesize($file);
$size1=$size-1;
//获得字节范围
if(isset($_SERVER['HTTP_RANGE'])) {
   list($name, $range) = explode("=",$_SERVER['HTTP_RANGE']);
   $length=$size1-$range;
   header("HTTP/1.1 206 Partial Content");  //http协议头状态码,表示以部分内容传输
   header("Content-Range: bytes ".$range."-".$size1."/".$size);
}else{
   $length = $size;
}

header("Content-Length: $length");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename);

$fp=fopen($file,"rb");

//设定文件指针位置
fseek($fp,$range);

while(!feof($fp)){
   set_time_limit(0);
   echo fread($fp,1024);
   flush();
   ob_flush();
}
fclose($fp);
exit;
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn