[PHP] How to use fputcsv to instantly output a CSV file with a large amount of data

  • 23 July 2019
Post image

If you want to create a csv file with php, you will probably use the fputcsv function. However, when writing a csv file with hundreds of thousands of rows and more than 20MB of data, it will take a lot of time.

This time I needed to write out a file in PHP fputcsv within the normal response time (3 seconds or less), and there was somewhat little information available, so I summarized what I found.

How to do a super slow fputcsv.

If you look it up on the Internet, you’ll find all of the following methods. This is almost all they write.

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

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

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

fclose($fp);

When I tried to export a file with more than 300,000 lines of data using the above fputcsv, it took about 30 seconds. That’s because the data is put into a flie one line at a time.

When I google it, all I get are articles about this process. Are the articles referring to this insane? Or are you assuming a ridiculously high spec server?

Writing CSV files with Stream

The above method is slow because it writes one line at a time. In other words, if you can flash a csv file in one shot, the writing process will be much faster.

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

Allocate the contents to the stream php://temp, and put the contents to be written by stream_get_contents at once. It took me less than 3 seconds to do this. In other words, we can write 10 times faster than before.

It is definitely better to use this stream (php://output) when responding to a csv file via HTTP.

gRPC witrh Rust the fastest?Pursuing 1ms
gRPC witrh Rust the fastest?Pursuing 1ms
Menu The time between request and response should be as short as …
> Read More

(Article migrated from another blog)

You May Also Like