PHP fputcsv() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fputcsv() writes one row of data to an open file in CSV format. CSV means comma separated values.

You pass an array of values and fputcsv() joins them with commas. It adds quotes around values that need them, such as values with commas, spaces or quotes inside.

Syntax

Call fputcsv() with the values you need. Start with this line.

PHP
fputcsv($handle, ['Asha', 'Chennai']);
CallWhat it does
fputcsv($fp, ['Asha', 'Chennai'])Writes the row Asha,Chennai followed by a new line.
fputcsv($fp, ['Smith, John', 'IT'])Writes "Smith, John",IT because the comma needs quotes.
fputcsv($fp, ['id', 'name'], ';')Uses a semicolon instead of a comma as the separator.

fputcsv() adds the quotes for you, so do not add your own. Each call writes exactly one line. Use fgetcsv() to read the rows back. Since PHP 8.1 you can also choose the end-of-line text with the last parameter.

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

Parameters

ParameterDescription
streamRequired. An open file handle returned by fopen(), for example in w or a mode. You can also use php://output.
fieldsRequired. An array of values that make up one row.
separatorOptional. A single character that separates the values. The default is a comma.
enclosureOptional. A single character used to wrap values that need quotes. The default is a double quote.
escapeOptional. A single character used as the escape character. The default is a backslash. Pass an empty string to turn it off.
eolOptional. The text added at the end of the line. The default is a new line. Available since PHP 8.1.

Return Value

Return ValueDescription
intReturns the length of the string that was written, in bytes.
falseReturns false on failure.

PHP Examples

Example 1 — Write a Small CSV File

Write a header row and two data rows, then print the file.

PHP
<?php
$file = __DIR__ . '/people.csv';

$fp = fopen($file, 'w');
fputcsv($fp, ['name', 'city']);
fputcsv($fp, ['Asha', 'Chennai']);
fputcsv($fp, ['Ravi', 'Mumbai']);
fclose($fp);

echo file_get_contents($file);

unlink($file);

How It Works

  1. fopen() opens a new CSV file for writing.
  2. Each fputcsv() call turns one array into one line of comma separated text.
  3. fclose() saves and closes the file.
  4. file_get_contents() reads the file back so we can print it.

Example 2 — Values with Commas and Quotes

fputcsv() wraps tricky values in quotes and doubles any quote marks inside them.

PHP
<?php
$out = fopen('php://output', 'w');

fputcsv($out, ['Smith, John', 'He said "Hi"']);

fclose($out);

How It Works

  1. php://output lets us write straight to the screen instead of a file.
  2. The first value has a comma, so it is wrapped in double quotes.
  3. The second value contains quotes, so each one is doubled and the whole value is wrapped.
  4. A CSV reader will understand this line as exactly two columns.

Example 3 — Export Rows with a Semicolon

Loop over an array of records and write each one with a different separator.

PHP
<?php
$rows = [
    ['id', 'name', 'price'],
    [1, 'Pen', '1.50'],
    [2, 'Notebook', '3.25'],
];

$out = fopen('php://output', 'w');

foreach ($rows as $row) {
    fputcsv($out, $row, ';');
}

fclose($out);

How It Works

  1. The $rows array holds a header row and two product rows.
  2. foreach sends one row at a time to fputcsv().
  3. The third argument ; replaces the comma, which many European spreadsheets expect.
  4. Numbers and text are written the same way, because everything is turned into text.

Frequently Asked Questions

It formats an array as one CSV line and writes it to an open file.
Open a file with fopen() in w mode, call fputcsv() once for each row, and close the file with fclose().
Pass it as the third argument, for example fputcsv($fp, $row, ';') or fputcsv($fp, $row, "\t").
Set the Content-Type and Content-Disposition headers, open php://output, and call fputcsv() for each row.
Use fgetcsv() in a loop. It reads one line at a time and returns it as an array.

Next: fputs()

fputs() writes a string to an open file. It is an alias of fwrite(), so both functions do .

fputs() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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