PHP Excel Memory Limit of 2GB Exhausted reading a 286KB file

此生再无相见时 提交于 2019-12-31 04:07:24

问题


Addtitional info: I'm running this from the command line. CentOS 6, 32GB Ram total, 2GB Memory for PHP. I tried increasing the memory limit to 4GB, but now I get a Fatal error: String size overflow. PHP maximum string size is 2GB.

My code is very simple test code:

$Reader = new SpreadsheetReader($_path_to_files . 'ABC.xls');
$i = 0;
foreach ($Reader as $Row)
{   $i++;
    print_r($Row);
if($i>10) break;

}

And it is only to print 10 rows. And that is taking 2 Gigabytes of memory?

The error is occuring at line 253 in excel_reader2.php

Inside class OLERead, inside function read($sFilenName)

Here is the code causing my exhaustion:

if ($this->numExtensionBlocks != 0) {

        $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4;

    }



    for ($i = 0; $i < $bbdBlocks; $i++) { // LINE 253

        $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);

        $pos += 4;

    }

回答1:


I solved the problem. It turned out to be somewhat unrelated to the php code.

The program I am writing downloads .xls, .xlsx, and .csv files from email and FTP. The .xls file that was causing the memory overflow was downloaded in ASCII mode instead of Binary.

I changed my default to binary mode, and added a check that changes it to ASCII mode for .csv files.

I still find it strange that the program creates a 2GB string because of that. If there are no line breaks in the binary file, then I can see perhaps how the entire file might end up in one string. But the file is only 286KB. So, that's strange.



来源:https://stackoverflow.com/questions/18545217/php-excel-memory-limit-of-2gb-exhausted-reading-a-286kb-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!