首頁 >後端開發 >php教程 >好用的php header下載函數

好用的php header下載函數

WBOY
WBOY原創
2016-07-25 08:54:40905瀏覽
複製程式碼
  1. /**

  2. * 傳送檔案
  3. *
  4. * @param string $fileName 檔案名稱或路徑
  5. * @param string $fancyName 自訂的檔案名稱,為空白則使用filename
  6. * @param booleanName 自訂的檔案名稱,為空則使用filename
  7. * @param booleanName 自訂的檔案名稱,為空則使用filename
  8. * @param booleanName $forceDownload 是否強制下載
  9. * @param integer $speedLimit 速度限制,單位為位元組,0為不限制,不支援windows伺服器
  10. * @param string $$contentType 檔案類型,預設為application/octet- stream
  11. *
  12. * @return boolean
  13. */
  14. function sendFile($fileName, $fancyName = ' ', $forceDownload = true, $speedLimit = 0, $contentType = '')
  15. {
  16. if (!is_read($fileName))
  17. {
  18. header("HTTP/1.1 404 找不到") ;
  19. return false;
  20. }
  21. $fileStat = stat($fileName);
  22. $lastModified = $fileStat['mtime'];
  23. ; $md5 = md5($fileStat['mtime'] .'='.$fileStat['ino'] .'='.$fileStat['size']);

  24. $etag = '"' . $md5 '-' .crc32($md5) '"';
  25. header('最後修改時間: ' .gmdate("D, d M Y H:i:s", $lastModified) . ' GMT');
  26. header("ETag: $etag");
  27. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $ > {

  28. header("HTTP/1.1 304 未修改");
  29. return true;
  30. }
  31. if (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) & strtotime(P.D. ) ']) {
  32. header("HTTP /1.1 304 未");
  33. return true;
  34. }
  35. if (isset($_SERVER['HTTP_IF_NONE_MATMATCH'] ) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
  36. {
  37. header("HTTP/1.1 304 未修改");
  38. return true;
  39. }
  40. }
  41. if ($ffancyName == '')
  42. {
  43. $fancyName = 基本名稱($檔名);
  44. }
  45. if ($contentType == '')

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

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

  63. {
  64. $startPos = $fileSize - $endPos;
  65. $endPos = $fileSize - 1;
  66. }
  67. else if ($ endPos == '')
  68. {
  69. $endPos = $fileSize - 1;
  70. }
  71. $startPos = $startPos $endPos = $endPos > $檔案大小 - 1 ? $fileSize - 1 : $endPos;
  72. $length = $endPos - $startPos 1;
  73. if ($length {
  74. return false;
  75. }
  76. {
  77. return false;
  78. }
  79. $ contentLength = $length;
  80. $isPartial = true;
  81. }
  82. }
  83. // 傳送headers

  84. if ($isPartial)
  85. {
  86. header ('HTTP/1.1 206 部分內容');
  87. header("Content-Range: bytes $startPos-$endPos/$fileSize ");
  88. }

  89. else
  90. {
  91. header("HTTP/1.1 200 OK");
  92. $startPos = 0;
  93. $endPos = $contentLength - 1;
  94. }
  95. header ('Pragma: 儲存') ;
  96. header('Cache-Control: public, Must-revalidate, max-age=0');
  97. header('Accept-Ranges: bytes');
  98. header ('內容類型:' . $contentType);
  99. header('內容長度:' . $contentLength);
  100. if ($forceDownload)

  101. {
  102. header('Content-Disposition:attachment; filename="' .rawurlencode($fancyName). '"');//漢字自動轉為編碼URL
  103. header( 'Content-Disposition:attachment; filename="' . $fancyName.'"');
  104. }
  105. header("Content-Transfer-Encoding:binary");
  106. // header 函數實作檔案下載

    $bufferSize = 2048; if ($ speedLimit != 0) { $packetTime = Floor($bufferSize * 1000000 / $speedLimit); }

  107. $bytesSent = 0;
  108. $fp = fopen($fileName, "rb");
  109. fseek($fp, $startPos);
  110. //fpassthru($fp); p >
  111. while ($bytesSent {

  112. if ($speedLimit != 0)
  113. {
  114. if ($speedLimit != 0)
  115. {
  116. { list($usec, $sec) = 爆炸(" ", microtime());
  117. $outputTimeStart = ((float)$usec (float)$sec);
  118. } // bbs.it-home.org
  119. $readBufferSize = $contentLength - $bytesSent
  120. $ 寬度尺寸$ contentLength - $bytesSent : $bufferSize;
  121. $buffer = fread($fp, $readBufferSize);
  122. echo $buffer;
  123. ob_flush();
  124. lush();
  125. $bytesSent = $readBufferSize;
  126. if ($speedLimit != 0)

  127. {
  128. list($usec, $sec) = Explode(" ", microtime());
  129. $outputTimeEnd = ((float)$usec (float)$sec);
  130. $useTime = ((float) $outputTimeEnd - (float) $outputTimeStart) * 1000000;

  131. $sleepTime = round($packetTime - $useTime);
  132. if ($sleepTime > 0)
  133. {
  134. usleep($sleepTime);
  135. }
  136. }
  137. }
  138. }
回傳true;
}?>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn