ホームページ  >  記事  >  バックエンド開発  >  便利なphpヘッダーダウンロード機能

便利なphpヘッダーダウンロード機能

WBOY
WBOYオリジナル
2016-07-25 08:54:40849ブラウズ
  1. /**

  2. * ファイルを送信
  3. *
  4. * @param string $fileName ファイル名またはパス
  5. * @param string $fancyName カスタマイズされたファイル名、空の場合はファイル名を使用します
  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']) < $lastModified)
  31. {
  32. header("HTTP/1.1 304 Not Modified");
  33. return true;
  34. }
  35. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)
  36. {
  37. header("HTTP/1.1 304 Not Modified");
  38. return true;
  39. }
  40. if ( $fancyName == '')
  41. {
  42. $fancyName = ベース名($fileName);
  43. }

  44. if ($contentType == '')

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

  49. $contentLength = $fileSize;

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

  61. if ($startPos == '')

  62. {
  63. $startPos = $fileSize - $endPos;
  64. $endPos = $fileSize - 1;
  65. }
  66. else if ($endPos == '')
  67. {
  68. $endPos = $ファイルサイズ - 1;
  69. }
  70. $startPos = $startPos < 0 ? 0 : $startPos;
  71. $endPos = $endPos > $fileSize - 1 ? $fileSize - 1 : $endPos;
  72. $length = $endPos - $startPos + 1;
  73. if ($length {
  74. return false;
  75. }
  76. $contentLength = $length;
  77. $isPartial = true;
  78. }
  79. }

  80. // ヘッダーを送信します

  81. if ($isPartial)
  82. {
  83. header('HTTP/1.1 206 Partial Content');
  84. header("Content-Range: bytes $startPos-$endPos/$fileSize");

  85. }

  86. else
  87. {
  88. header("HTTP/1.1 200 OK");
  89. $startPos = 0;
  90. $endPos = $contentLength - 1;
  91. }
  92. header('Pragma: キャッシュ');
  93. header('Cache-コントロール: public、must-revalidate、max-age=0');
  94. header('Accept-Ranges: bytes');
  95. header('Content-type: ' . $contentType);
  96. header('Content-Length: ' . $contentLength);

  97. if ($forceDownload)

  98. {
  99. header('Content-Disposition:attachment; filename="' . rawurlencode($fancyName). '"');//汉字自动转為URL编码
  100. header('Content-Disposition:attachment ; filename="' . $fancyName. '"');
  101. }
  102. header("Content-Transfer-Encoding: binary");
  103. // header関数实现文件下ダウンロード

  104. $bufferSize = 2048;

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

  113. while ($bytesSent < $contentLength && !feof($fp) && connection_status() == 0 )

  114. {
  115. if ($speedLimit != 0)
  116. {
  117. list($usec, $sec) =explode(" " 、マイクロタイム());
  118. $outputTimeStart = ((float)$usec + (float)$sec);
  119. } // bbs.it-home.org
  120. $readBufferSize = $contentLength - $bytesSent < $bufferSize ? $contentLength - $bytesSent : $bufferSize;
  121. $buffer = fread($fp, $readBufferSize);
  122. echo $buffer;
  123. ob_flush();
  124. flash();
  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. return true;
  139. }
  140. ?>

复制代码


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。