加载csv文件时出错:巨大的文本节点内存不足

时间:2013-04-14 16:19:46

标签: php csv nginx phpexcel

使用PHPExcel加载38MB .csv文件时,即使我将huge text node: out of memory设置为1024M,我也收到php memory_limit错误。

知道如何加载大型.csv文件吗?

错误

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代码

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

2 个答案:

答案 0 :(得分:1)

您可以使用fgetcsv并逐行加载csv。

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

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

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

fclose($f);

答案 1 :(得分:0)

使用PHPExcel读取大量Excel文件问题的可能解决方案

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

其中一个答案建议以大块的形式阅读大量文件。