简体   繁体   中英

PHPExcel Fatal error: Allowed memory size

I use PHPexcel to open a .xlsx file (on ovh mutualized server) and encountered problems that I solved.

I have a new problem when saving the the modified file :

"Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 49 bytes) in /home/observatvu/www/libraries/phpexcel/library/PHPExcel/Cell.php on line 870"

I read many questions and answers on internet and tried some solutions like :

  • memory_limit in .htaccess => problem on the server, it does'nt work
  • ini_set('memory_limit','512M') => I have the message above... with other ini_set values I have other sizes of memory error but no saving of the file.
  • I can't modify php.ini

I tried to write setPreCalculateFormulas(false) during saving the file but always the same problem.

Please someone could help me to find a working solution ?

Thank you

If you tried

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize' => '1024MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); 

Then it wouldn't work.

You have a limit of 536,870,912 (512MB) for your PHP

The line

$cacheSettings = array( 'memoryCacheSize' => '1024MB');

is telling PHPExcel to use 1024MB of PHP memory before switching to using php://temp for caching.... that's what the memory element of the argument name memoryCacheSize means.

Use a lower value for the memoryCacheSize value than the amount of your PHP memory limit

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize' => '256MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); 

If you can't make it work with PHPExcel's caching system, you can give Spout a try: https://github.com/box/spout . It was designed to work with files of any size without causing memory or time limit issues.

All you need is 10MB of memory available and you can read all the XLSX files you want :)

This type of error can occur when passing an incorrect cell letter to a phpExcel function such as:

$objPHPExcel->getActiveSheet()->setCellValue($cell, $value);

Be sure not to increment column letters like this:

chr(ord($col) + 1);

Best to use a custom increment function like:

//$start = 'A'
private function _incrementCol($start, $offset)
{
    $result = $start;
    for($i = 1; $i <= $offset; $i++) {
        $result++;
    }
    return $result;
}

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