PHP可恢复下载
我在互联网上找到了这个功能:PHP可恢复下载,php,download,resume-download,Php,Download,Resume Download,我在互联网上找到了这个功能: function download_file($file,$ctype, $is_resume=TRUE) { //First, see if the file exists if (!is_file($file)) { die("<b>404 File not found!</b>"); } //Gather relevent info about file $size = f
function download_file($file,$ctype, $is_resume=TRUE)
{
//First, see if the file exists
if (!is_file($file))
{
die("<b>404 File not found!</b>");
}
//Gather relevent info about file
$size = filesize($file);
$fileinfo = pathinfo($file);
//workaround for IE filename bug with multiple periods / multiple dots in filename
//that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe
$filename = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ?
preg_replace('/\./', '%2e', $fileinfo['basename'], substr_count($fileinfo['basename'], '.') - 1) :
$fileinfo['basename'];
//check if http_range is sent by browser (or download manager)
if($is_resume && isset($_SERVER['HTTP_RANGE']))
{
list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if ($size_unit == 'bytes')
{
//multiple ranges could be specified at the same time, but for simplicity only serve the first range
//http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
list($range, $extra_ranges) = explode(',', $range_orig, 2);
}
else
{
$range = '';
}
}
else
{
$range = '';
}
//figure out download piece from range (if set)
list($seek_start, $seek_end) = explode('-', $range, 2);
//set start and end based on range (if set), else set defaults
//also check for invalid ranges.
$seek_end = (empty($seek_end)) ? ($size - 1) : min(abs(intval($seek_end)),($size - 1));
$seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);
//add headers if resumable
if ($is_resume)
{
//Only send partial content header if downloading a piece of the file (IE workaround)
if ($seek_start > 0 || $seek_end < ($size - 1))
{
header('HTTP/1.1 206 Partial Content');
}
header('Accept-Ranges: bytes');
header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$size);
}
//headers for IE Bugs (is this necessary?)
//header("Cache-Control: cache, must-revalidate");
//header("Pragma: public");
header('Content-Type: ' . $ctype);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: '.($seek_end - $seek_start + 1));
//open the file
$fp = fopen($file, 'rb');
//seek to start of missing part
fseek($fp, $seek_start);
//start buffered download
while(!feof($fp))
{
//reset time limit for big files
set_time_limit(0);
print(fread($fp, 1024*8));
flush();
ob_flush();
}
fclose($fp);
exit;
}
函数下载文件($file,$ctype,$is\u resume=TRUE)
{
//首先,查看文件是否存在
如果(!is_file($file))
{
死(“404文件找不到!”);
}
//收集有关文件的相关信息
$size=filesize($file);
$fileinfo=pathinfo($file);
//IE文件名错误的解决方法,文件名中有多个句点/多个点
//这将在文件名中添加方括号-例如,setup.abc.exe将变成setup[1].abc.exe
$filename=(strstrstr($\服务器['HTTP\用户\代理],'MSIE'))?
preg\u replace('/\./','%2e',$fileinfo['basename'],substr\u count($fileinfo['basename'],'.')-1):
$fileinfo['basename'];
//检查http_范围是否由浏览器(或下载管理器)发送
if($is_resume&&isset($_SERVER['HTTP_RANGE']))
{
列表($size\u unit,$range\u orig)=分解('=',$服务器['HTTP\u range',2);
如果($size\u unit=='bytes')
{
//可以同时指定多个范围,但为简单起见,仅服务于第一个范围
//http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
列表($range,$extra_ranges)=分解(“,”,$range_orig,2);
}
其他的
{
$range='';
}
}
其他的
{
$range='';
}
//从范围中找出下载件(如果已设置)
列表($seek\u start,$seek\u end)=分解('-',$range,2);
//根据范围设置开始和结束(如果已设置),否则设置默认值
//还要检查无效的范围。
$seek_end=(空($seek_end))?($size-1):min(abs(intval($seek_end)),($size-1));
$seek_start=(空($seek_start)|$seek_end0 |$$seek_end<($size-1))
{
标题(“HTTP/1.1 206部分内容”);
}
标题('Accept-Ranges:bytes');
标题('Content-Range:bytes.$seek\u start.-'.$seek\u end./.$size);
}
//IE bug的标题(是否有必要?)
//标头(“缓存控制:缓存,必须重新验证”);
//标题(“Pragma:public”);
标题('Content-Type:'.$ctype);
标题('Content-Disposition:attachment;filename=“.”.$filename.'”);
标题('Content-Length:'。($seek_end-$seek_start+1));
//打开文件
$fp=fopen($file,'rb');
//寻找缺失部分的开始
fseek($fp,$seek\u start);
//启动缓冲下载
而(!feof($fp))
{
//重置大文件的时间限制
设置时间限制(0);
打印(fread($fp,1024*8));
冲洗();
ob_flush();
}
fclose($fp);
出口
}
下载工作正常,但当我使用下载管理器(IDM等)并想暂停下载以便稍后继续时,我无法完成。该函数应该允许这样做(查看内容范围
标题),但由于某些原因,它不起作用
有人知道为什么吗?我该如何修复它
我在互联网上找到了很多功能,但都没有正常工作。@see-从第658行开始,举个例子
可以说,可恢复下载需要正确的HTTP头处理,特别是“连接:关闭”一个