本文章给大家来总结几种实现下载短点续传程序功能,这些函数中主要是用到了php的header函数,有需要了解的朋友可进入参考。
例如:下载时输出 下载文件大小,文件名等等
前提是.htaccess文件的配置需要添加一句
setenv no-gzip dont-vary
就是针对文件不进行压缩处理
例1
代码如下 复制代码
例2
代码如下 复制代码
$fname = './mmldzg.mp3';
$fp = fopen($fname,'rb');
$fsize = filesize($fname);
if (isset($_server['http_range']) && ($_server['http_range'] != ) && preg_match(/^bytes=([0-9]+)-$/i, $_server['http_range'], $match) && ($match[1] $start = $match[1];
} else {
$start = 0;
}
@header(cache-control: public); @header(pragma: public);
if ($star--> 0) {
fseek($fp, $start);
header(http/1.1 206 partial content);
header(content-length: . ($fsize - $start));
header(content-ranges: bytes . $start . - . ($fsize - 1) . / . $fsize);
} else {
header(content-length: $fsize);
header(accept-ranges: bytes);
}
@header(content-type: application/octet-stream);
@header(content-disposition: attachment;filename=mmdld.mp3);
fpassthru($fp);
fpassthru() 函数输出文件指针处的所有剩余数据。
该函数将给定的文件指针从当前的位置读取到 eof,并把结果写到输出缓冲区
上面两个实例对中文支持不好,下面这个函数很好的解决了这个问题
代码如下 复制代码
/**
* php-http断点续传实现
* @param string $path: 文件所在路径
* @param string $file: 文件名
* @return void
*/
function download($path,$file) {
$real = $path.'/'.$file;
if(!file_exists($real)) {
return false;
}
$size = filesize($real);
$size2 = $size-1;
$range = 0;
if(isset($_server['http_range'])) {
header('http /1.1 206 partial content');
$range = str_replace('=','-',$_server['http_range']);
$range = explode('-',$range);
$range = trim($range[1]);
header('content-length:'.$size);
header('content-range: bytes '.$range.'-'.$size2.'/'.$size);
} else {
header('content-length:'.$size);
header('content-range: bytes 0-'.$size2.'/'.$size);
}
header('accenpt-ranges: bytes');
header('application/octet-stream');
header(cache-control: public);
header(pragma: public);
//解决在ie中下载时中文乱码问题
$ua = $_server['http_user_agent'];
if(preg_match('/msie/',$ua)) {
$ie_filename = str_replace('+','%20',urlencode($file));
header('content-dispositon:attachment; filename='.$ie_filename);
} else {
header('content-dispositon:attachment; filename='.$file);
}
$fp = fopen($real,'rb+');
fseek($fp,$range);
while(!feof($fp)) {
set_time_limit(0);
print(fread($fp,1024));
flush();
ob_flush();
}
fclose($fp);
}
/*end of php*/
http://www.bkjia.com/phpjc/444606.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/444606.htmltecharticle本文章给大家来总结几种实现下载短点续传程序功能,这些函数中主要是用到了php的header函数,有需要了解的朋友可进入参考。 例如:下载...