<?php
/*
为了方便大家,我把我遇到的问题总结如下.可能你们还会遇到其他的问题,那就要根据实际情况解决了,但是总结一句话: 养成良好的编程习惯和风格 多动手 多动脑 ,问题就会慢慢解决掉.
1. 在php文件中<-?php ... ?->标志外不能有多余空格和换行,否则读取的压缩包头信息不正确
2.在使用fread函数读取数据后,不能使用flush()或ob_flush()函数清空缓存
3.不能使用header("Content-Range: $from-$to fsize");
header("Content-Length: $content_size");
这两个命令预定文件大小,否则文件读取不完整
他奶奶的就这3条花了我一晚上时间才整明白,日了......
*/
function file_resume($file){
//First, see if the file exists
if (!is_file($file)) { die("<b>404 File not found!</b>"); }
//Gather relevent info about file
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/x-zip-compressed"; break;
case "rar": $ctype="application/x-rar"; break;
default: $ctype="application/force-download";
}
//Begin writing headers
header("Cache-Control:");
header("Cache-Control: public");
//Use the switch-generated Content-Type
header("Content-Type: $ctype");
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
# 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
$iefilename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
header("Content-Disposition: attachment; filename=\"$iefilename\"");
//header("Content-Range: $from-$to fsize"); 加上压缩包头信息不正确
//header("Content-Length: $content_size"); 加上压缩包头信息不正确
} else {
header("Content-Disposition: attachment; filename=\"$filename\"");
//header("Content-Range: $from-$to fsize"); 加上压缩包头信息不正确
//header("Content-Length: $content_size"); 加上压缩包头信息不正确
}
header("Accept-Ranges: bytes");
//header('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT');
$size=filesize($file);
//open the file
$fp=fopen("$file","rb");
/ ek to start of missing part
fseek($fp,$range);
//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;
}
?>