作者:shouhou1213_491 | 来源:互联网 | 2013-04-22 17:17
<?php/***PHP-HTTP断点续传实现*@paramstring$path:文件所在路径*@paramstring$file:文件名*@returnvoid*/functiondownload($path,$file){$real..."><
/**
* PHP-HTTP断点续传实现
* @param string $path: 文件所在路径
* @param string $file: 文件名
* @return void
*/
function download($path,$file) {
$real = $path.&#39;/&#39;.$file;
if(!file_exists($real)) {
return false;
}
$size = filesize($real);
$size2 = $size-1;
$range = 0;
if(isset($_SERVER[&#39;HTTP_RANGE&#39;])) {
header(&#39;HTTP /1.1 206 Partial Content&#39;);
$range = str_replace(&#39;=&#39;,&#39;-&#39;,$_SERVER[&#39;HTTP_RANGE&#39;]);
$range = explode(&#39;-&#39;,$range);
$range = trim($range[1]);
header(&#39;Content-Length:&#39;.$size);
header(&#39;Content-Range: bytes &#39;.$range.&#39;-&#39;.$size2.&#39;/&#39;.$size);
} else {
header(&#39;Content-Length:&#39;.$size);
header(&#39;Content-Range: bytes 0-&#39;.$size2.&#39;/&#39;.$size);
}
header(&#39;Accenpt-Ranges: bytes&#39;);
header(&#39;application/octet-stream&#39;);
header("Cache-control: public");
header("Pragma: public");
//解决在IE中下载时中文乱码问题
$ua = $_SERVER[&#39;HTTP_USER_AGENT&#39;];
if(preg_match(&#39;/MSIE/&#39;,$ua)) {
$ie_filename = str_replace(&#39;+&#39;,&#39;%20&#39;,urlencode($file));
header(&#39;Content-Dispositon:attachment; filename=&#39;.$ie_filename);
} else {
header(&#39;Content-Dispositon:attachment; filename=&#39;.$file);
}
$fp = fopen($real,&#39;rb+&#39;);
fseek($fp,$range);
while(!feof($fp)) {
set_time_limit(0);
print(fread($fp,1024));
flush();
ob_flush();
}
fclose($fp);
}
/*End of PHP*/