虽然php 中的header()函数下载文件不支持断点续传功能但有时我们还真需要此功能,如我们下载txt,图片文件时如果直接是个连接估计是直接打开了而不是下载了,那么我们可如何实现下载呢,代码如下:
- <?php
-
-
-
-
-
-
- header("Content-type:text/html;charset=utf-8");
-
- download('web/www.phpfensi.com .txt', 'txt文件下载');
-
- function download($file, $down_name){
-
- $suffix = substr($file,strrpos($file,'.'));
- $down_name = $down_name.$suffix;
-
-
- if(!file_exists($file)){
- die("您要下载的文件已不存在,可能是被删除");
- }
- $fp = fopen($file,"r");
- $file_size = filesize($file);
-
- header("Content-type: application/octet-stream");
- header("Accept-Ranges: bytes");
- header("Accept-Length:".$file_size);
- header("Content-Disposition: attachment; filename=".$down_name);
- $buffer = 1024;
- $file_count = 0;
-
- while(!feof($fp) && $file_count < $file_size){
- $file_con = fread($fp,$buffer);
- $file_count += $buffer;
- echo $file_con;
- }
- fclose($fp);
- }
-
- ?>
|