Unique validation with Node.js class-validator
This is about class-validator, nodejs. https://github.com/typestack/class-validator Installation class-validator This is a validation library that is …
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.
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?
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.
(Article migrated from another blog)
This is about class-validator, nodejs. https://github.com/typestack/class-validator Installation class-validator This is a validation library that is …
Set cookies with Axios from a browser If you want to use axios to send the browser’s SessionCookie and other information directly to the server, …