php Header函数实现下载短点续传程序
本文章给大家来总结几种实现下载短点续传程序功能,这些函数中主要是用到了php的header函数,有需要了解的朋友可进入参考。
例如:下载时输出 下载文件大小,文件名等等
前提是.htaccess文件的配置需要添加一句
SetEnv no-gzip dont-vary
就是针对文件不进行压缩处理
例1
<?php
function download($file_dir, $file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
$file_dir = chop($file_dir); //去掉路径中多余的空格
//得出要下载的文件的路径
if ($file_dir !=) {
$file_path = $file_dir;
if (substr($file_dir, strlen($file_dir) - 1, strlen($file_dir)) != /) $file_path.= /;
$file_path.= $file_name;
} else {
$file_path = $file_name;
}
//判断要下载的文件是否存在
if (!file_exists($file_path)) {
echo '对不起,你要下载的文件不存在。';
return false;
}
$file_size = filesize($file_path);
@header("Cache-control: public");
@header("Pragma: public");
//header("Content-Encoding: gzip");
@header("Content-Type: application/octetstream");
header("Content-Length: $file_size");
Header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=" . $file_name);
$fp = fopen($file_path, "r");
fpassthru($fp);
return true;
}
download('路径参数', '文件名');
?>例2
<?php
$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] < $fsize)) {
$start = $match[1];
} else {
$start = 0;
}
@header("Cache-control: public");
@header("Pragma: public");
if ($start > 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
/**
* 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.phprm.com/frame/php1005371.html
欢迎转载!但请带上文章地址^^