在大文件上使用fgetcsv耗尽内存

时间:2017-02-06 18:03:54

标签: php fopen fgetcsv

我收到以下错误: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes)

它失败的行是来自此代码块的while (($dataInputRecord = fgetcsv($inputFile, $maxRowLength, ",")) !== FALSE)

    if (($inputFile = fopen($fileName, "r")) !== FALSE) {
        while (($dataInputRecord = fgetcsv($inputFile, $maxRowLength, ",")) !== FALSE) {
            // do stuff
        }
        fclose($inputFile);
    }

整个文件是32MB。玩$maxRowLength大小并没有帮助。我确信问题是文件大小,因为将其截断为8 KB可以解决问题。我怎么能不关心文件大小?

编辑:看起来我的老板误导我了解循环中的代码应该做什么

2 个答案:

答案 0 :(得分:0)

您可以通过更改php.ini中的memory_limit

来增加内存限制

答案 1 :(得分:0)

试试ini_set('memory_limit', '256M');

  

134217728字节= 128 MB

相关问题