doumei1908 2016-06-10 12:01
浏览 96
已采纳

在使用PHP的IIS中,文件下载随机失败

We have a website that streams files to our customers. Now, we started having files bigger than 300Mb and for no apparent reasons, the downloads are randomly failing. We get a "the download was interrupted" error.

We logged in to the server and figured out that the PHP processes were sometimes closing when a download would start but there's nothing significant either in the event viewer or in the logs.

Here's the code that delivers the downloads:

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 '';
    }

Here's our setup:

  • Windows Server 2008 R2
  • IIS 7.5
  • PHP 5.5.30

What I tried:

I tried to increase the memory_limit to 1024Mb, to increase the timeout, to increase the script execution time and we didn't get any benefits from increasing those.

From the event viewer, we are getting a lot of errors when the downloads fail:

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
ativerd.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

Edit 1

It looks like modifying the buffer an putting it at 12000 works, but not at 11999. What kind of sorcery is this?

  • 写回答

2条回答 默认 最新

  • doyp9057 2016-06-13 15:07
    关注

    Apparently, putting the $fileBuffer at 12000 works but not at 11999... But why?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码