好用的php header下载函数

php中文网
发布: 2016-07-25 08:54:40
原创
1042人浏览过
  1. /**

  2. * 发送文件
  3. *
  4. * @param string $filename 文件名称或路径
  5. * @param string $fancyname 自定义的文件名,为空则使用filename
  6. * @param boolean $forcedownload 是否强制下载
  7. * @param integer $speedlimit 速度限制,单位为字节,0为不限制,不支持windows服务器
  8. * @param string $$contenttype 文件类型,默认为application/octet-stream
  9. *
  10. * @return boolean
  11. */
  12. function sendfile($filename, $fancyname = '', $forcedownload = true, $speedlimit = 0, $contenttype = '')
  13. {
  14. if (!is_readable($filename))
  15. {
  16. header("http/1.1 404 not found");
  17. return false;
  18. }
  19. $filestat = stat($filename);
  20. $lastmodified = $filestat['mtime'];
  21. $md5 = md5($fileStat['mtime'] .'='. $fileStat['ino'] .'='. $fileStat['size']);

  22. $etag = '"' . $md5 . '-' . crc32($md5) . '"';
  23. header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT');
  24. header("ETag: $etag");
  25. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified)

  26. {
  27. header("HTTP/1.1 304 Not Modified");
  28. return true;
  29. }
  30. if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) {
  31. header("HTTP/1.1 304 Not Modified");
  32. return true;
  33. }
  34. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
  35. {
  36. header("HTTP/1.1 304 Not Modified");
  37. return true;
  38. }
  39. if ($fancyName == '')
  40. {
  41. $fancyName = basename($fileName);
  42. }
  43. if ($contentType == '')

  44. {
  45. $contentType = 'application/octet-stream';
  46. }
  47. $fileSize = $fileStat['size'];
  48. $contentLength = $fileSize;

  49. $isPartial = false;
  50. if (isset($_SERVER['HTTP_RANGE']))
  51. {
  52. if (preg_match('/^bytes=(d*)-(d*)$/', $_SERVER['HTTP_RANGE'], $matches))
  53. {
  54. $startPos = $matches[1];
  55. $endPos = $matches[2];
  56. if ($startPos == '' && $endPos == '')
  57. {
  58. return false;
  59. }

    立即学习PHP免费学习笔记(深入)”;

  60. if ($startPos == '')

  61. {
  62. $startPos = $fileSize - $endPos;
  63. $endPos = $fileSize - 1;
  64. }
  65. else if ($endPos == '')
  66. {
  67. $endPos = $fileSize - 1;
  68. }
  69. $startPos = $startPos $endPos = $endPos > $fileSize - 1 ? $fileSize - 1 : $endPos;
  70. $length = $endPos - $startPos + 1;
  71. if ($length {
  72. return false;
  73. }
  74. $contentLength = $length;
  75. $isPartial = true;
  76. }
  77. }
  78. // send headers

    某地板超炫企业网站1.1
    某地板超炫企业网站1.1

    1、演示:以截图为准 2、程序说明 程序试用后台:http://你的域名/admin/login.asp 后台登陆帐号:admin 密码:admin123 说明: 这个是基于asp+access的企业网站源码,数据库已设有有防下载,网站更安全 要修改网站,自定义你自己要的页面,和美化页面都是你自己完成,网站源码程序完整,后台功能强大。 调试运行环境:要安装IIS服务器(IIS的安装和配置,安装好

    某地板超炫企业网站1.1 0
    查看详情 某地板超炫企业网站1.1
  79. if ($isPartial)
  80. {
  81. header('HTTP/1.1 206 Partial Content');
  82. header("Content-Range: bytes $startPos-$endPos/$fileSize");
  83. }

  84. else
  85. {
  86. header("HTTP/1.1 200 OK");
  87. $startPos = 0;
  88. $endPos = $contentLength - 1;
  89. }
  90. header('Pragma: cache');
  91. header('Cache-Control: public, must-revalidate, max-age=0');
  92. header('Accept-Ranges: bytes');
  93. header('Content-type: ' . $contentType);
  94. header('Content-Length: ' . $contentLength);
  95. if ($forceDownload)

  96. {
  97. header('Content-Disposition: attachment; filename="' . rawurlencode($fancyName). '"');//汉字自动转为URL编码
  98. header('Content-Disposition: attachment; filename="' . $fancyName. '"');
  99. }
  100. header("Content-Transfer-Encoding: binary");
  101. // header函数实现文件下载
  102. $bufferSize = 2048;

  103. if ($speedLimit != 0)
  104. {
  105. $packetTime = floor($bufferSize * 1000000 / $speedLimit);
  106. }
  107. $bytesSent = 0;
  108. $fp = fopen($fileName, "rb");
  109. fseek($fp, $startPos);
  110. //fpassthru($fp);
  111. while ($bytesSent {

  112. if ($speedLimit != 0)
  113. {
  114. list($usec, $sec) = explode(" ", microtime());
  115. $outputTimeStart = ((float)$usec + (float)$sec);
  116. } // bbs.it-home.org
  117. $readBufferSize = $contentLength - $bytesSent $buffer = fread($fp, $readBufferSize);
  118. echo $buffer;
  119. ob_flush();
  120. flush();
  121. $bytesSent += $readBufferSize;
  122. if ($speedLimit != 0)

  123. {
  124. list($usec, $sec) = explode(" ", microtime());
  125. $outputTimeEnd = ((float)$usec + (float)$sec);
  126. $useTime = ((float) $outputTimeEnd - (float) $outputTimeStart) * 1000000;

  127. $sleepTime = round($packetTime - $useTime);
  128. if ($sleepTime > 0)
  129. {
  130. usleep($sleepTime);
  131. }
  132. }
  133. }
  134. return true;
  135. }
  136. ?>
复制代码


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号