Answer the question
In order to leave comments, you need to log in
How to split large csv file using PHP?
Hello.
In general, there is a huge CSV file with a size of 1.5 GB .
Which I need to parse and write a migration for it.
As usual, I wrote a simple script for processing the file
, but unfortunately the shell hangs all the
time .
Answer the question
In order to leave comments, you need to log in
What is the essence of the problem?
Reading
php.net/manual/en/function.fgetcsv.php
Writing
php.net/manual/en/function.fputcsv.php
Considering csv is pure data, one line of data per line, then the solution is -split -l
here I can give a well-documented function for reading tables saved in txt with tabs in excel in windows
public function get_rows($file_path, $start = 0, $length = 0)
{
$handle = fopen($file_path, 'r');
$rows = array();
$i = 0;
while ( !feof($handle) )
{
$row = fgets($handle);
if ( $i >= $start && $i < $start + $length )
{
$row = trim($row);
if ( strlen($row) )
{
$cells = explode("\t", iconv('windows-1251', 'utf-8', $row));
foreach ( $cells as $n => $cell )
if ( substr($cell, 0, 1) == '"' && substr($cell, -1) == '"' )
$cells[$n] = str_replace('""', '"', substr($cell, 1, strlen($cell) - 2));
$rows[] = $cells;
}
}
$i++;
}
fclose($handle);
return $rows;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question