解決PHP超大檔案下載,斷點續傳下載的方法詳解_php技巧

來源:互聯網
上載者:User
最近匯出的時候出現一個php記憶體溢出的問題,原因就是在於下載的時候讀取產生的臨時檔案過大,PHP記憶體無法容納,一開如是想到更改PHP記憶體限制,但是這個只是一個緩兵之計,於是想到了另外一個方法是把檔案分次讀取,並下載。
以下是原始碼:
複製代碼 代碼如下:

<?php
$sourceFile = "1.tmp"; //要下載的臨時檔案名稱
$outFile = "使用者訂單.xls"; //下載儲存到用戶端的檔案名稱
$file_extension = strtolower(substr(strrchr($sourceFile, "."), 1)); //擷取副檔名
//echo $sourceFile;
if (!ereg("[tmp|txt|rar|pdf|doc]", $file_extension))exit ("非法資源下載");
//檢測檔案是否存在
if (!is_file($sourceFile)) {
die("<b>404 File not found!</b>");
}
$len = filesize($sourceFile); //擷取檔案大小
$filename = basename($sourceFile); //擷取檔案名稱字
$outFile_extension = strtolower(substr(strrchr($outFile, "."), 1)); //擷取副檔名
//根據副檔名 指出輸出瀏覽器格式
switch ($outFile_extension) {
case "exe" :
$ctype = "application/octet-stream";
break;
case "zip" :
$ctype = "application/zip";
break;
case "mp3" :
$ctype = "audio/mpeg";
break;
case "mpg" :
$ctype = "video/mpeg";
break;
case "avi" :
$ctype = "video/x-msvideo";
break;
default :
$ctype = "application/force-download";
}
//Begin writing headers
header("Cache-Control:");
header("Cache-Control: public");
//設定輸出瀏覽器格式
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=" . $outFile);
header("Accept-Ranges: bytes");
$size = filesize($sourceFile);
//如果有$_SERVER['HTTP_RANGE']參數
if (isset ($_SERVER['HTTP_RANGE'])) {
/*Range頭域 Range頭域可以請求實體的一個或者多個子範圍。
例如,
表示頭500個位元組:bytes=0-499
表示第二個500位元組:bytes=500-999
表示最後500個位元組:bytes=-500
表示500位元組以後的範圍:bytes=500-
第一個和最後一個位元組:bytes=0-0,-1
同時指定幾個範圍:bytes=500-600,601-999
但是伺服器可以忽略此要求標頭,如果無條件GET包含Range要求標頭,響應會以狀態代碼206(PartialContent)返回而不是以200 (OK)。
*/
// 斷點後再次串連 $_SERVER['HTTP_RANGE'] 的值 bytes=4390912-
list ($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
//if yes, download missing part
str_replace($range, "-", $range); //這句幹什麼的呢。。。。
$size2 = $size -1; //檔案總位元組數
$new_length = $size2 - $range; //擷取下次下載的長度
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length"); //輸入總長
header("Content-Range: bytes $range$size2/$size"); //Content-Range: bytes 4908618-4988927/4988928 95%的時候
} else {
//第一次串連
$size2 = $size -1;
header("Content-Range: bytes 0-$size2/$size"); //Content-Range: bytes 0-4988927/4988928
header("Content-Length: " . $size); //輸出總長
}
//開啟檔案
$fp = fopen("$sourceFile", "rb");
//設定指標位置
fseek($fp, $range);
//虛幻輸出
while (!feof($fp)) {
//設定檔案最長執行時間
set_time_limit(0);
print (fread($fp, 1024 * 8)); //輸出檔案
flush(); //輸出緩衝
ob_flush();
}
fclose($fp);
exit ();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.