简体   繁体   中英

Error loading csv file: Huge text node out of memory

While loading a 38MB .csv file using PHPExcel, I am getting a huge text node: out of memory error even though I have set the php memory_limit to 1024M.

Any idea how the large .csv file can be loaded?

Error

Error loading file "data.csv": Warning: DOMDocument::loadHTMLFile(): xmlSAX2Characters: huge text node: out of memory in /var/www/site/data.csv, line: 264094 in /var/www/site/vendor/CodePlex/PHPExcel/PHPExcel/Reader/HTML.php line 458

PHP Code

ini_set('memory_limit', '1024M');

$inputFileName = '/var/www/site/data.csv';
$phpExcel = new PHPExcel;

//  Read your Excel workbook
try 
{
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName); 
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} 
catch(Exception $e) 
{
     die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

php.ini

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128M

You may use fgetcsv and load csv line by line.

$file = '/path/filename.csv';

$f = fopen($file, 'rb');

while( $cols = fgetcsv($f) ) 
{
  // $cols[0]; // first column of the current row
}

fclose($f);

Possible solutions on reading issue of high volume Excel files with PHPExcel

How to read large worksheets from large Excel files (27MB+) with PHPExcel?

One of the answers suggests reading huge file in chunks.

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