PHPExcel Fatal error: Allowed memory size - php

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;
}

Related

"Allowed memory size" error when there's enough memory

I have
PHP Fatal error: Allowed memory size of 13241728 bytes exhausted (tried to allocate 24 bytes)
script (php) file is 400kb, output is 250kb
It opens (and unserializes) several files, each of them is no more then 8mb (total 32Mb).
For example:
while (($file = readdir($dir))!=false) {
$inf=file_get_contents($file);
$inf=unserialize($inf);
......
}
I'm using fast-cgi.
Why there's not enough memory when it should be enough?
I've been setting it in php.ini from 12Mb, 16, 32, 50 (same error) to 80Mb (wow it works)
Now it is set to -1 but I want to figure out why this happens and how to solve it (maybe unset some vars or something)
this is because php doesn t know how to properly unserialize $inf .
it's looping in a infinite way .
What is in the file you re trying to get ?
i think this is because you trying to save the unserialized variable into itself .
unserialize is recurisve ...
so you should do :
$variable = unserialize ( $inf );
instead of
$inf = unserialize ( $inf );
official php doc : http://php.net/unserialize

PHPExcel_IOFactory::load Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) [duplicate]

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

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

PHP: Reading huge amount of excel file causing fatal error

i am facing a problem.
I need to read a .xls file of about 10MB. i write a php code that works fine when i read small .xls file. but when i try to read large file then the browser shows "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1032 bytes) in C:\wamp\www\student\ExcelRes\PHPExcel\Cell.php on line 1126"
Here is my code.
<?php
ini_set('memory_limit', '128M');
set_include_path(get_include_path() . PATH_SEPARATOR . 'ExcelRes/');
include 'PHPExcel/IOFactory.php';
$inputFileName = 'ru_unit_H_all.xls';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
echo $sheetData['20007'][ 'K']; //row colomn
?>
Error message should be self explaining:
"Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1032 bytes) in C:\wamp\www\student\ExcelRes\PHPExcel\Cell.php on line 1126"
You just simply ran out of memory reserved for execution of one script.
You may increase your memory_limit using ini_set() to solve this issue.
Note: using 128MB isn't enough, because 134217728B = ~128MB still causes that error. Try using 512MB.
There's no memory effective implementation of Excel reader/writer for PHP that I know of.
The memory available to the script has been exausted. By default I believe each script has a limit of 8MB of memory allocated to it. You are attempting to read 10MB file, and as such, there is not enough memory to process the requst and it fails.
You can try increasing the amount of memory available, by using memory_limit setting.
This can be done globally for all scripts in the php.ini settings file or on a per script basis using
ini_set('memory_limit','16M');
Where 16M is 16 Megabytes of memory.
In addition to possibly increasing memory, you should also be looking at the cell caching options provided by PHPExcel for precisely this purpose, as described in the section of the developer documentation entitled "cell caching" (section 4.2.1)
EDIT
And your use of toArray() is going to build the array you're requesting in PHP memory as well, adding extra overhead - consider iterating over the worksheet a row at a time rather than loading it twice into memory (once as a PHPExcel object and once as your array)
Finally i solve my problem by using example12 code.
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'ExcelRes/');
include 'PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
$inputFileName = 'ru_unit_H_all.xls';
class chunkReadFilter implements PHPExcel_Reader_IReadFilter
{
private $_startRow = 0;
private $_endRow = 0;
public function setRows($startRow)
{
$this->_startRow = $startRow;
}
public function readCell($column, $row, $worksheetName = '')
{
if (($row == 1) || ($row >= $this->_startRow ))
{
return true;
}
return false;
}
}
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$chunkFilter = new chunkReadFilter();
$objReader->setReadFilter($chunkFilter);
$chunkFilter->setRows(22000);
$objPHPExcel = $objReader->load($inputFileName);
echo $objPHPExcel->getActiveSheet()->getCell('K22000')->getValue();
}
?>

Fatal error from phpmailer class

I have made a php page that sends an email message with multiple attachments.
The loop which i used to attach multiple attachments and to check the size of attachments is ,
foreach(array_keys($_FILES['attach']['name']) as $key)
{
$filesize = $_FILES['attach']['size'][$key];
$extention = pathinfo ($_FILES['attach']['name'][$key] ,PATHINFO_EXTENSION
);
$name=$_FILES['attach']['name'][$key];
$data=($_FILES['attach']['tmp_name']);
$totalsize = $totalsize + $filesize;
if($totalsize > 10000000) //10mb10000000
{$err="<font color=#990000 size=1>File exceeded maximum allowed limit of 10
Mb</font>";}
else{
$source = $_FILES['attach']['tmp_name'][$key];
$filename = $_FILES['attach']['name'][$key];
$mail->AddAttachment($source, $filename);
}
}//end Foreach loop
But when i try to attach a large file i get this error from the phpmailer class.
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate
7355049 bytes) in /var/www/dev01/maiarn/Email/class.phpmailer.php on line 1677
Any body who can guide me Please.
You might want to increase the PHP memory limit. If you're working on your development machine, you could search for the php.ini file and modify the memory_limit (which often defaults to 16M). Change that to f.e. 128M and restart your webserver.
If you want to see it change, you can use the following line to show the configuration currently in use:
<?php phpinfo(); ?>
Let PHP use more memory on this script only by using the following PHP code:
ini_set(‘memory_limit’,’64M’);

Categories