PHP – How to export CSV file immediately from huge data with fputcsv

2019-10-07

One of way creating csv with php is fputcsv function. But it takes a long time when export csv from huge data over 20MB.

I needed to export files with fputcsv of PHP, and it must be completed operation within 3 sec.

Slow fputcsv

There are almost same infomations the following on the web.

$list = [
    ["hoge", "fuga"],
    //Omit
]

$fp = fopen('export.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

It takes 30 second to export csv files from data over 300,000 rows with above fputcsv. Because it write data one row by row.

Put files with stream

The above way is very slowly because writing data one row by row. Basically, it will be fast by putting csv files at once.

$stream = fopen('php://temp', 'w');
foreach ($csv as $line) {
    fputcsv($stream, $line);
}
rewind($stream); // rewind pointer
$content = stream_get_contents($stream);
fclose($stream);
file_put_contents('export.csv', $content);

It store data to “php://temp" are read-write streams, then write data that are gotten with stream_get_contents are put at once. This way takes within 3s.

To write to stream are very fast. When it response with csv files, writing stream(php://output) is used usually.