Home PHP Filesystem fgetcsv() PHP fgetcsv() Function Definition In PHP, fgetcsv() reads one line from an open CSV file and splits it into an array of fields. It understands quotes, so a value like "Smith, John" stays in one piece.
Instead of cutting each line with explode() yourself, fgetcsv() parses the line for you and handles commas inside quoted values. Call it in a loop to read a whole CSV file row by row.
Syntax Call fgetcsv() with the values you need. Start with this line.
$row = fgetcsv($handle, null, ',', '"', ''); Call What it does fgetcsv($handle, null, ',', '"', '')Reads the next row and returns its fields as an array. fgetcsv($handle, null, ';', '"', '')Reads a row from a file that uses semicolons as separators. while (($row = fgetcsv($handle, null, ',', '"', '')) !== false) { ... }Reads every row of the file, one by one.
A blank line is returned as an array with a single null value, not as false. In PHP 8.4, relying on the default $escape value is deprecated, so the examples pass an empty string to turn the escape feature off.
Library declarations fgetcsv(resource $stream, ?int $length = null, string $separator = ",", string $enclosure = "\"", string $escape = "\\"): array|false Parameters Parameter Description stream Required. An open file stream, usually from fopen(), pointing at a CSV file. length Optional. The longest line to read in bytes. The default null means no limit. In PHP 7 and earlier, 0 meant no limit. separator Optional. The field separator, exactly one character. The default is a comma. enclosure Optional. The field quote character, exactly one character. The default is a double quote. escape Optional. The escape character. Pass an empty string to disable it. Relying on the default is deprecated in PHP 8.4.
Return Value Return Value Description array An array with one item for each field of the row. false Returned at the end of the file or on an error.
Important
A blank line gives [null], not false and not an empty array, so check the row before you use $row[1]. Also pass the separator and enclosure explicitly when your file is not comma-separated, and always open the file before the loop and close it afterwards.
PHP Examples 1. Read a CSV File Row by Row 2. Blank Lines Return [null] 3. Quoted Fields with Commas Example 1 — Read a CSV File Row by Row Create a small CSV file with a header, read the header first and then print every data row.
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetcsv.csv';
$csv = 'name,age' . PHP_EOL . 'Alice,30' . PHP_EOL . 'Bob,25' . PHP_EOL;
file_put_contents($path, $csv);
$handle = fopen($path, 'r');
$header = fgetcsv($handle, null, ',', '"', '');
echo 'Columns: ', implode(', ', $header), PHP_EOL;
while (($row = fgetcsv($handle, null, ',', '"', '')) !== false) {
echo $row[0], ' is ', $row[1], ' years old', PHP_EOL;
}
fclose($handle);
unlink($path); Columns: name, age
Alice is 30 years old
Bob is 25 years old How It Works The first fgetcsv() call reads the header row and returns ['name', 'age']. implode() joins the header names so you can print them. The while loop calls fgetcsv() again for each data row. When the file ends, fgetcsv() returns false and the loop stops. Example 2 — Blank Lines Return [null] An empty line in the CSV file does not return false. It returns an array with one null value, which you should skip.
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetcsv_edge.csv';
file_put_contents($path, 'a,b' . PHP_EOL . PHP_EOL . 'c,d' . PHP_EOL);
$handle = fopen($path, 'r');
$n = 0;
while (($row = fgetcsv($handle, null, ',', '"', '')) !== false) {
$n++;
echo $n, ': ', json_encode($row), PHP_EOL;
}
fclose($handle);
unlink($path); 1: ["a","b"]
2: [null]
3: ["c","d"] How It Works The file has three lines, and the middle one is empty. For the empty line, fgetcsv() returns an array that holds a single null value. json_encode() prints the arrays so the [null] row is easy to see. In real code, skip rows where $row === [null] before using the fields. Frequently Asked Questions What does fgetcsv() do in PHP? It reads one line from an open CSV file and returns the fields of that line as an array. It correctly handles quoted values that contain commas.
How do I read a whole CSV file in PHP? Open the file with fopen() and call fgetcsv() inside a while loop that stops when it returns false. Close the file with fclose() at the end.
What does fgetcsv() return for an empty line? An array with a single null value, which is [null]. It does not return false, so skip such rows yourself.
How do I read a CSV file that uses semicolons? Pass the separator as the third argument: fgetcsv($handle, null, ';', '"', ''). Many spreadsheets in Europe export files that way.
What is the difference between fgetcsv() and str_getcsv()? fgetcsv() reads from an open file and moves the pointer line by line, while str_getcsv() parses a CSV string you already have in memory.
Next: fgets() fgets() reads one line from an open file. It returns the line as a string, including the l.
fgets() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion