Allowed memory size in Phpexcel

Asked

Viewed 243 times

0

I need to upload xls and its reading for insertion in db, if a file reaches the megabytes is always returned to me the message:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in

Code for reading the xls

require('library/php-excel-reader/excel_reader2.php');
require('library/SpreadsheetReader.php');

$inputFileName = '/home/uploads/'.$nome_arquivo;
$header = 'Conta';


    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objReader->setReadDataOnly(true);
    $objPHPExcel = $objReader->load($inputFileName);
    //Get worksheet and built array with first row as header
    $objWorksheet = $objPHPExcel->getActiveSheet();
    //excel with first row header, use header as key
    if($header){
        $highestRow = $objWorksheet->getHighestRow();
        $highestColumn = $objWorksheet->getHighestColumn();
        $headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
        $headingsArray = $headingsArray[1];
        $r = -1;
        $namedDataArray = array();
        for ($row = 2; $row <= $highestRow; ++$row) {
            $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
            if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
                ++$r;
                foreach($headingsArray as $columnKey => $columnHeading) {
                    $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
                }
            }
        }
    }
    else{
        //excel sheet with no header
        $namedDataArray = $objWorksheet->toArray(null,true,true,true);
    } 

Error always returns in line:

$namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];

that’s inside the foreach.

I already added at the beginning of the file:

set_time_limit(0); ini_set('memory_limit', '2024M');

Unsuccessful

1 answer

0

Go on php.ini and changes the value of upload_max_filesize. Restart the server before testing again!

  • This is @Jonathan, client hosting is shared here, php.ini editing is not available. In this case, I created php.ini with upload_max_filesize = 250M and I’ve also added php_value memory_limit 254Min htaccess, both unsuccessfully

Browser other questions tagged

You are not signed in. Login or sign up in order to post.