简体   繁体   中英

PHPExcel throws Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes)

I am using PHPExcel (found here: https://github.com/PHPOffice/PHPExcel ). If i try to read more than approximately 2000 rows then it shows memory error as follows.

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /home/sample/PHPExcelReader/Classes/PHPExcel/worksheet.php on line 89

My Excel data range is A1:X2000

Below is my code used to read the excel.

ini_set('memory_limit', '-1');
/** Include path **/
        set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');


    /** PHPExcel_IOFactory */
    include $unsecured_param['home_dir'].'APIs/PHPExcelReader/Classes/PHPExcel/IOFactory.php';
    $inputFileName = $target;  // File to read
    //echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
    try {
        $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
    } catch(Exception $e) {
        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }

    $sheetData = $objPHPExcel->getActiveSheet()->rangeToArray('A1:X2000', null, true, true, true)
    //store data into array..
    $i=0;$j=0;$max_rows=0;$max_columns=0;
    foreach($sheetData as $rec)
    {
        foreach($rec as $part)
        {//echo "items[$j][$i]=" ; echo $part;echo "<br>";
            $items[$j][$i]=$part; $i=$i+1;
            if($j==0) {$max_columns=$i;}
        }
        $j=$j+1;$i=0;
    }
    $max_rows=$j;

Could any one please let me know how to overcome this issue ?

Consider using cell caching to reduce the memory required to hold the workbook in memory, as described in section 4.2.1 of the developer documentation

And consider not using toArray() and then using that to build another array in memory.... doing this is really using a lot of memory to hold duplicated data, when you could simply loop through the rows and columns of the worksheet to do what you need

This error means that the PHP file that you are running has exceeded the allowed size in memory for PHP on your server. You can edit your PHP.ini file to allow your PHP files to allocate more space in memory when they are running, which may assist in this, but at the same time, if you are running a 32 bit Linux OS on your server for whatever reason, there is a hard cape of 3.5GB that the process can take up, so even allocating more than that, it will still fail and therefore cause a similar issue.

In cases such as this, it really comes down to the fact that the amount of data that you are trying to pull is too large and you need to scale it back somehow. It isn't necessarily an issue with the code, but rather how much data you are actually attempting to show/process.

Using google, I managed to find that the amount of memory that your noting (134217728 bytes), matches with the 128MB default that PHP.ini uses for memory_limit. Changing the value in the ini natively, will resolve this issue. If unable to do that, then you need to somehow limit the amount of data that you pull in one time.

Information: http://ca1.php.net/manual/en/ini.core.php#ini.memory-limit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM