Manipulate CSV Files in PHP
CSV for Comma-Separated Values is a well established and common file format to import and export data. Let's learn how to read and write CSV files in a modern and efficient way in PHP.
The Old-Fashioned Way
PHP contains native functions to read CSV content from a file pointer.
Read CSV rows from a file using `fgetcsv`:
Write CSV rows into a file using `fputcsv`:
This approach is totally fine, but some modern libraries can make our life easier and our code more readable.
The Modern Way
Let's install and use the excellent league/csv library, which provides a clean and straightforward CSV manipulation API.
Read CSV rows from a file using the`Reader`:
Write CSV rows into a file using the`Writer`:
Big Files and Memory Usage
The trick to parse big CSV files by keeping a low memory usage is to never load all data in memory.
Luckily, both methods we explored allow us to iterate and stream the content.
Let's take an example of a file containing 1M of lines; this file weights almost 400MB.
Let's load all its content:
And here is the result:
We load our 1M lines using only 6MB of memory (I'm using PHP8).
By streaming properly the data reading and the processing you want to apply, you can keep the memory usage very low.
You can find all the code and examples in this GitHub repository.
It's packaged as a simple Symfony project, a set of commands, it also comes with a Docker image. 🐋