[英]File downloads are randomly failing in IIS with PHP
我們有一個網站,可將文件流式傳輸給客戶。 現在,我們開始擁有大於300Mb的文件,並且沒有明顯的原因,下載隨機失敗。 我們收到“下載中斷”錯誤。
我們登錄到服務器,發現下載開始時PHP進程有時會關閉,但事件查看器或日志中沒有任何重要意義。
這是提供下載的代碼:
public function downloadAction($asset, $csrfToken) {
if ($this->securityContext->isCsrfProtectionTokenValid($csrfToken) === FALSE) {
throw new \TYPO3\Flow\Security\Exception\AccessDeniedException();
}
// This is a : \TYPO3\Flow\Resource\Resource
$resource = $asset->getResource();
$stream = $resource->getStream();
if (FALSE === $stream) {
$this->throwStatus(500);
}
try {
$streamMetadata = stream_get_meta_data($stream);
$modified = filemtime($streamMetadata['uri']);
$filename = $asset->getResource()->getFilename();
$this->response->setStatus(200);
$this->response->setHeader('Content-Type', 'application/octet-stream');
$this->response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
$this->response->setHeader('Last-Modified', gmdate('r', $modified));
$this->response->setHeader('Content-Length', $asset->getResource()->getFileSize());
$this->response->setHeader('Content-transfer-encoding', 'binary');
$this->response->sendHeaders();
@set_time_limit(86400);
$fileBuffer = 8192;
while (!feof($stream)) {
print(fread($stream, $fileBuffer));
ob_flush();
flush();
}
} finally {
fclose($stream);
}
return '';
}
這是我們的設置:
我試過的
我試圖將memory_limit增加到1024Mb,以增加超時時間,以增加腳本執行時間,而增加它們並沒有帶來任何好處。
從事件查看器中,下載失敗時會遇到很多錯誤:
Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7afa2
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x0000000000d94b98
Faulting process id: 0x26a0
Faulting application start time: 0x01d1c3157fec8818
Faulting application path: c:\windows\system32\inetsrv\w3wp.exe
Faulting module path: unknown
Report Id: 9ea4b22a-2f09-11e6-8366-00155d016700
Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7afa2
Faulting module name: nativerd.dll, version: 7.5.7601.17855, time stamp: 0x4fc85321
Exception code: 0xc0000005
Fault offset: 0x000000000000f4d3
Faulting process id: 0x2170
Faulting application start time: 0x01d1c310490342e2
Faulting application path: c:\windows\system32\inetsrv\w3wp.exe
Faulting module path: c:\windows\system32\inetsrv\nativerd.dll
Report Id: 9ea48b1a-2f09-11e6-8366-00155d016700
Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7afa2
Faulting module name: iisfcgi.dll, version: 7.5.7601.17514, time stamp: 0x4ce7c6cb
Exception code: 0xc0000005
Fault offset: 0x0000000000007a47
Faulting process id: 0x28b8
Faulting application start time: 0x01d1c3016d933dec
Faulting application path: c:\windows\system32\inetsrv\w3wp.exe
Faulting module path: C:\Windows\System32\inetsrv\iisfcgi.dll
Report Id: 82a04d74-2f03-11e6-8366-00155d016700
編輯1
看起來好像修改緩沖區並使其在12000起作用,但在11999卻沒有。這是什么魔術?
您是否嘗試過用readfile()替換fread()? 它無需打開文件處理程序,而是通過php讀取和傳輸文件塊,而是直接將文件內容流式傳輸到Web服務器。
顯然,將$fileBuffer
為12000
可以,但是不能設置為11999
...但是為什么呢?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.