免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5625 | 回复: 4
打印 上一主题 下一主题

[下载] 文件自动下载 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-14 14:21 |只看该作者 |倒序浏览
我在使用自动下载文件时   
header("Content-Disposition: attachment; filename=$FileName.$Ext");
header("Pragma: no-cache");
  header("Expires: 0");
一般的浏览器是可以下载的 ?
但是有的浏览器装了像什么DUDU下载之类的插件,导致要下载的文件被事先拦截,但是通过那个工具下载的不是我所要的文件,而是一个php文件

[ 本帖最后由 HonestQiao 于 2005-12-14 14:33 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2005-12-15 10:25 |只看该作者
如果是rar之类的可以Header
其它的如txt可以用readfile

论坛徽章:
0
3 [报告]
发表于 2005-12-16 19:20 |只看该作者
<?php
$fsize = filesize($file);
$fname = basename($file);
$ftime = filemtime($file);

$range = $_SERVER['HTTP_RANGE'];
$r_start = 0;
$c_length = $fsize;

if($range) {
  $reg = "/bytes=([0-9]+)-([0-9]*)/";
  preg_match($reg, $range, $matches);

  $r_start = (int) $matches[1];
  $r_stop = (int) $matches[2];

  if ($r_stop < $r_start) $r_stop = $fsize - 1;
  $c_length = $r_stop - $r_start + 1;

  header('HTTP/1.1 206 Partial Content');
  header('Content-Range: bytes '.
    $r_start .  '-' . $r_stop . '/' . $fsize);
} else {
  header('HTTP/1.1 200 OK');
}

header('Last-Modified: '.gmstrftime('%a, %d %b %Y %T', $ftime).' GMT');
header('Content-Disposition: attachment; filename="'.$fname.'"');
header('Accept-Ranges: bytes');
header('Content-Type: application/x-unknown');
header('content-Transfer-Encoding: binary');
header('Content-Length: '.$c_length);
if ($fp = fopen($file, 'rb')) {
  fseek($fp, $r_start);
  for($len = $c_length; $len > 0 {
    $rlen = $len < 128000? $len : 128000;
    echo(fread($fp, $rlen));
    $len = $len - $rlen;
    set_time_limit(20);
  }
  fclose($fp);
}
clearstatcache();
exit;
?>

论坛徽章:
0
4 [报告]
发表于 2005-12-16 19:21 |只看该作者
支持断点续传。。。

论坛徽章:
0
5 [报告]
发表于 2008-04-30 07:21 |只看该作者
嗯,不错,功能强大,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP