Mam plik csv który zajmuje 140 MB, serwer nie daje rady jest "Out of memory" i zastanawiam sie czy nie da sie jakos partiami wczytywac pliku zeby tylko jego czesc byla w pamieci a pozniej kolejna czesc odczytac i przerobic ? Ewentualnie jakis inny sposob, jestem w kropce, juz 2 godziny nad tym siedze ... w komentarzach w manualu znalazlem klase CsvImporter która niby to robi, ale jak odczytam przed tym plikiem jakis jeszcze wczesniej, to tez mam "Out of memory". Moze daloby sie przerobic jakos ta klase zeby nie zajmowala tyle pamieci ?
<?php
class CsvImporter
{
private $fp;
private $parse_header;
private $header;
private $delimiter;
private $length;
//--------------------------------------------------------------------
function __construct($file_name, $parse_header=false, $delimiter="\t", $length=8000)
{
$this->fp = fopen($file_name, "r"); $this->parse_header = $parse_header;
$this->delimiter = $delimiter;
$this->length = $length;
// $this->lines = $lines;
if ($this->parse_header)
{
$this->header = fgetcsv($this->fp, $this->length, $this->delimiter); }
}
//--------------------------------------------------------------------
function __destruct()
{
if ($this->fp)
{
}
}
//--------------------------------------------------------------------
function get($max_lines=0)
{
//if $max_lines is set to 0, then get all the data
if ($max_lines > 0)
$line_count = 0;
else
$line_count = -1; // so loop limit is ignored
while ($line_count < $max_lines && ($row = fgetcsv($this->fp, $this->length, $this->delimiter)) !== FALSE) {
if ($this->parse_header)
{
foreach ($this->header as $i => $heading_i)
{
$row_new[$heading_i] = $row[$i];
}
$data[] = $row_new;
}
else
{
$data[] = $row;
}
if ($max_lines > 0)
$line_count++;
}
return $data;
}
//--------------------------------------------------------------------
}
?>