Processing CSV Files in PHP
Opening the CSV File
To open a CSV file, start by opening the file by using PHP function fopen.
|
|
Reading CSV Data
After opening the file (creating file pointer), cycle through the content using fgetcsv function. This function reads the file line by line and parse it based on fields in CSV.
|
|
Note: Second parameter of fgetcsv must be set to the biggest line in the CSV file in character. By default its value is zero but it would be faster if you know the longest line in your CSV file and specify it.
Complete Example
Here’s a complete example showing how to process a CSV file with headers:
|
|