Y
Y
yesworld2015-04-30 11:46:57
PHP
yesworld, 2015-04-30 11:46:57

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

4 answer(s)
E
egor_nullptr, 2015-04-30
@yesworld

split -C 100M file.csv

F
Fortop, 2015-04-30
@Fortop

What is the essence of the problem?
Reading
php.net/manual/en/function.fgetcsv.php
Writing
php.net/manual/en/function.fputcsv.php

L
Leonid Sysoletin, 2015-04-30
@sysoletin

Considering csv is pure data, one line of data per line, then the solution is -
split -l

6
65536, 2015-04-30
@65536

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;
}

you need to organize a queue and read in chunks of such a size that their processing fits into memory

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question