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

老子叫甜甜 提交于 2020-01-11 11:14:33

问题


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 ?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/21891248/phpexcel-throws-fatal-error-allowed-memory-size-of-134217728-bytes-exhausted-t

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