fgetcsv()
fgetcsv() — Gets line from file pointer and parse for CSV fields.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Similar to fgets() except that fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read.
Signature
fgetcsv(resource $stream, ?int $length = null, string $separator = ",", string $enclosure = "\"", string $escape = "\\"): array|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $stream | resource | A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen(). |
| $length | ?int | Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). Otherwise the line is split in chunks of length characters, unless the split would occur inside an enclosure. |
| $separator | string | The separator parameter sets the field separator. It must be a single byte character. |
| $enclosure | string | The enclosure parameter sets the field enclosure character. It must be a single byte character. |
| $escape | string | The escape parameter sets the escape character. It must be a single byte character or the empty string. The empty string ("") disables the proprietary escape mechanism. |
Return value
Returns an indexed array containing the fields read on success, or false on failure. Note: A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error. A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error. Note: Prior to PHP 8.1.0, the auto_detect_line_endings run-time configuration option could be enabled to help PHP properly recognize line endings when reading files created on Macintosh systems. This option has been deprecated as of PHP 8.1.0. If necessary, handle "\r" line breaks manually instead. Prior to PHP 8.1.0, the auto_detect_line_endings run-time ...
Example
A runnable snippet:
if (($handle = fopen("test.csv", "r")) !== false) { $row = 1; while (($fields = fgetcsv($handle, 1000, ",", "\"", "\\")) !== false) { $num = count($fields); echo "\n<p>$num fields in line $row:</p>\n<ol>"; for ($c = 0; $c < $num; $c++) { echo "\n\t<li>", $fields[$c], "</li>"; } echo "\n</ol>"; $row++; } fclose($handle); }
Changelog
| Version | Change |
|---|---|
| 8.4.0 | Relying on the default value of escape is now deprecated. |
| 8.3.0 | An empty string is returned instead of a string with a single null byte for the last field if it contains only an unterminated enclosure. |
| 8.0.0 | length is now nullable. |
| 7.4.0 | The escape parameter now also accepts an empty string to disable the proprietary escape mechanism. |
See also
fputcsv(), str_getcsv(), explode(), file(), pack()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page fgetcsv() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.