php超大檔案下載及斷點續傳下載的實現代碼

來源:互聯網
上載者:User
  1. $sourceFile = "jbxue.tmp"; //要下載的臨時檔案名稱
  2. $outFile = "使用者訂單.xls"; //下載儲存到用戶端的檔案名稱
  3. $file_extension = strtolower(substr(strrchr($sourceFile, "."), 1)); //擷取副檔名
  4. //echo $sourceFile;
  5. if (!ereg("[tmp|txt|rar|pdf|doc]", $file_extension))exit ("非法資源下載");
  6. //檢測檔案是否存在
  7. if (!is_file($sourceFile)) {
  8. die("404 File not found!");
  9. }
  10. $len = filesize($sourceFile); //擷取檔案大小
  11. $filename = basename($sourceFile); //擷取檔案名稱字
  12. $outFile_extension = strtolower(substr(strrchr($outFile, "."), 1)); //擷取副檔名
  13. //根據副檔名 指出輸出瀏覽器格式
  14. switch ($outFile_extension) {
  15. case "exe" :
  16. $ctype = "application/octet-stream";
  17. break;
  18. case "zip" :
  19. $ctype = "application/zip";
  20. break;
  21. case "mp3" :
  22. $ctype = "audio/mpeg";
  23. break;
  24. case "mpg" :
  25. $ctype = "video/mpeg";
  26. break;
  27. case "avi" :
  28. $ctype = "video/x-msvideo";
  29. break;
  30. default :
  31. $ctype = "application/force-download";
  32. }
  33. //Begin writing headers
  34. header("Cache-Control:");
  35. header("Cache-Control: public");
  36. //設定輸出瀏覽器格式
  37. header("Content-Type: $ctype");
  38. header("Content-Disposition: attachment; filename=" . $outFile);
  39. header("Accept-Ranges: bytes");
  40. $size = filesize($sourceFile);
  41. //如果有$_SERVER['HTTP_RANGE']參數
  42. if (isset ($_SERVER['HTTP_RANGE'])) {
  43. /*Range頭域 Range頭域可以請求實體的一個或者多個子範圍。
  44. 例如,
  45. 表示頭500個位元組:bytes=0-499
  46. 表示第二個500位元組:bytes=500-999
  47. 表示最後500個位元組:bytes=-500
  48. 表示500位元組以後的範圍:bytes=500-
  49. 第一個和最後一個位元組:bytes=0-0,-1
  50. 同時指定幾個範圍:bytes=500-600,601-999
  51. 但是伺服器可以忽略此要求標頭,如果無條件GET包含Range要求標頭,響應會以狀態代碼206(PartialContent)返回而不是以200 (OK)。
  52. */
  53. // 斷點後再次串連 $_SERVER['HTTP_RANGE'] 的值 bytes=4390912-
  54. list ($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
  55. //if yes, download missing part
  56. str_replace($range, "-", $range); //這句幹什麼的呢。。。。
  57. $size2 = $size -1; //檔案總位元組數
  58. $new_length = $size2 - $range; //擷取下次下載的長度
  59. header("HTTP/1.1 206 Partial Content");
  60. header("Content-Length: $new_length"); //輸入總長
  61. header("Content-Range: bytes $range$size2/$size"); //Content-Range: bytes 4908618-4988927/4988928 95%的時候
  62. } else {
  63. //第一次串連
  64. $size2 = $size -1;
  65. header("Content-Range: bytes 0-$size2/$size"); //Content-Range: bytes 0-4988927/4988928
  66. header("Content-Length: " . $size); //輸出總長
  67. }
  68. //開啟檔案
  69. //edit bbs.it-home.org
  70. $fp = fopen("$sourceFile", "rb");
  71. //設定指標位置
  72. fseek($fp, $range);
  73. //虛幻輸出
  74. while (!feof($fp)) {
  75. //設定檔案最長執行時間
  76. set_time_limit(0);
  77. print (fread($fp, 1024 * 8)); //輸出檔案
  78. flush(); //輸出緩衝
  79. ob_flush();
  80. }
  81. fclose($fp);
  82. exit ();
  83. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.