Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP fputcsv() and fgetcsv() functions

Reference for PHP fputcsv() and fgetcsv(): write arrays as CSV rows and read rows back, quoting, delimiters, the escape parameter, blank lines and common mistakes.

fputcsv() writes one array as a CSV row to an open handle, adding quotes and doubling quote characters where needed. fgetcsv() does the reverse: it reads one line from a handle and parses it into an array of strings. Both understand fields that contain commas, quotes and even line breaks.

Use them instead of explode(",", ...) and string concatenation, which break on the first field that contains a comma or a quote.

PHP
$h = fopen("people.csv", "w");
fputcsv($h, ["Ada", "Lovelace", "London, UK"], escape: "");
fclose($h);

Try it

PHP Writing rows with quotes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Reading rows back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
fputcsv(
    resource $stream, array $fields, string $separator = ",",
    string $enclosure = "\"", string $escape = "\\", string $eol = "\n"
): int|false

fgetcsv(
    resource $stream, ?int $length = null, string $separator = ",",
    string $enclosure = "\"", string $escape = "\\"
): array|false
Name Description
$stream An open file handle.
$fields (fputcsv) Values to write. Scalars are converted to strings; null becomes an empty field.
$separator A single character between fields. Use "\t" or ";" for other dialects.
$enclosure The quote character.
$escape The escape character. Pass "" to get standard RFC 4180 behavior.
$eol (fputcsv) Line ending written after the row.
$length (fgetcsv) Optional maximum line length; null means unlimited.

Return value: fputcsv() returns the number of bytes written or false. fgetcsv() returns an array of strings, [null] for a blank line, or false at end of file or on error.

Usage notes

  • Pass escape: "". The default escape character \ is a legacy behavior that can corrupt fields ending in a backslash. Leaving the argument out is deprecated in PHP 8.4, so be explicit.
  • Blank lines give [null], not an empty array. Check for it before indexing the result.
  • Everything comes back as a string. Convert numbers and dates yourself.
  • Header rows are ordinary rows. Read the first with fgetcsv() and combine it with each row via array_combine().
  • Line count is not row count. A quoted field can span lines, so do not count lines with fgets().
  • Short or long rows have fewer or more items than the header; array_combine() throws ValueError on a mismatch, so validate first.
  • Excel and the byte order mark. A file starting with a UTF-8 BOM puts it in the first field name; strip it with ltrim($s, "\xEF\xBB\xBF").
  • Formula injection. If the CSV will be opened in a spreadsheet, prefix values that start with =, +, - or @ to keep them from being run as formulas.
  • From a string: use str_getcsv().