File io 读取文件时允许的内存耗尽

File io 读取文件时允许的内存耗尽,file-io,nginx,apc,php,File Io,Nginx,Apc,Php,我正在使用PHP脚本(类似于下面给出的脚本)来流文件。 (服务器堆栈包括nginx+php-fpm+) nginx日志中经常记录以下错误 [error] 18391#0: *13673875 FastCGI sent in stderr: "PHP message: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) in /home/linux/src/l

我正在使用PHP脚本(类似于下面给出的脚本)来流文件。
(服务器堆栈包括nginx+php-fpm+)


nginx日志中经常记录以下错误

[error] 18391#0: *13673875 FastCGI sent in stderr: "PHP message: PHP Fatal error:
Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) in
/home/linux/src/library/file.php on line XX" while reading response header from
upstream, client: XXX.XXX.XXX.XXX, server: example.com, request: 
"GET /file/download HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000",
host: "example.com"
为什么使用如此简单的脚本,php会耗尽128MB的内存?

试试这个:

function stream_file($file)
{
    $fp = fopen($file, "r");
    while ($buffer = fgets($fp, 1024))
    {
        echo $buffer;
    }
    fclose($fp);
}

readfile
替换为
stream\u file
功能对我很有效。我还将文件大小添加到了标题中

header('Content-Length: ' . $file->getSize());
while(true)
。。。PHP脚本(以及其他任何语言)中的一个坏主意,不管您找到什么借口来证明它。
header('Content-Length: ' . $file->getSize());