PHP Excel Memory Limit of 2GB Exhausted reading a

2019-03-02 02:27发布

问题:

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.