虽然php 中的header()函数下载文件不支持断点续传功能但有时我们还真需要此功能,如我们下载txt,图片文件时如果直接是个连接估计是直接打开了而不是下载了,那么我们可如何实现下载呢,代码如下:
01
02
03
/**
04
* 文件下载
05
*
06
**/
07
08
header("Content-type:text/html;charset=utf-8");
09
10
download('web/ .txt', 'txt文件下载');
11
12
function download($file, $down_name){
13
14
$suffix = substr($file,strrpos($file,'.')); //获取文件后缀
15
$down_name = $down_name.$suffix; //新文件名,就是下载后的名字
16
17
//判断给定的文件存在与否
18
if(!file_exists($file)){
19
die("您要下载的文件已不存在,可能是被删除");
20
}
21
$fp = fopen($file,"r");
22
$file_size = filesize($file);
23
//下载文件需要用到的头
24
header("Content-type: application/octet-stream");
25
header("Accept-Ranges: bytes");
26
header("Accept-Length:".$file_size);
27
header("Content-Disposition: attachment; filename=".$down_name);
28
$buffer = 1024;
29
$file_count = 0;
30
//向浏览器返回数据
31
while(!feof($fp) && $file_count < $file_size){
32
$file_con = fread($fp,$buffer);
33
$file_count += $buffer;
34
echo $file_con;
35
}
36
fclose($fp);
37
}
38
39
?>
阅读(320) | 评论(0) | 转发(0) |