[英]Vagrant + Apache + PHP running extremely slowly on Windows
[英]Downloadscript extremely slowly
我用javascript和php編寫了一個下載腳本。 它可以工作,但是如果我要下載一個大文件(例如1GB的zip文件),它的時間太長了,無法完成請求。 我認為這與我讀取文件有關。 如果是這樣,您知道如何更快地獲取它嗎?
注意 :我需要一個標頭,以強制下載圖像,pdf,任何類型的文件類型的原因。
JS很簡單。 看這個:
function downloadFile(file){
document.location.href = "script.php?a=downloadFile&b=."+ file;
}
PHP很簡單:
function downloadFile($sFile){
#Main function
header('Content-Type: '.mime_content_type($sFile));
header('Content-Description: File Transfer');
header('Content-Length: ' . filesize($sFile));
header('Content-Disposition: attachment; filename="' . basename($sFile) . '"');
readfile($sFile);
}
switch($_GET['a']){
case 'downloadFile':
echo downloadFile($_GET['b']);
break;
}
我想緩沖是大文件的問題。 嘗試以小塊(例如兆字節)讀取文件,並在打印每個塊后調用flush函數以刷新輸出緩沖區。
編輯:嗯,好的,這是您應該嘗試的代碼示例:
function downloadFile($sFile){
#Main function
if ( $handle = fopen( $sFile, "rb" ) ) {
header('Content-Type: '.mime_content_type($sFile));
header('Content-Description: File Transfer');
header('Content-Length: ' . filesize($sFile));
header('Content-Disposition: attachment; filename="' . basename($sFile) . '"');
while ( !feof($handle) ) {
print fread($handle, 1048576);
flush();
}
fclose($handle);
} else {
header('Status: 404');
header('Content-Type: text/plain');
print "Can't find the requested file";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.