欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

好用的php header下载函数

程序员文章站 2024-02-11 16:14:22
...
  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. }
  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

  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. ?>
复制代码