Content · Web Development · PHP

PHP fputcsv() Function: Usage and Examples

Writes a field array as one CSV record into a writable stream with quoting where needed. Learn how to apply it with examples and common mistakes.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 admin

A common use for fputcsv is processing data without rebuilding the operation manually. Writes a field array as one CSV record into a writable stream with quoting where needed.

What Is fputcsv in PHP?


Writes a field array as one CSV record into a writable stream with quoting where needed.

Syntax:

fputcsv($stream, ?array $fields, ?string $separator = ..., ?string $enclosure = ..., ?string $escape = ..., ?string $eol = ...) : int|false

Parameters and Return Value


  • $stream — A required value input for this operation. Verify the allowed values in the examples and context below.
  • $fields — A required array input for this operation. Verify the allowed values in the examples and context below.
  • $separator — An optional string input for this operation. Verify the allowed values in the examples and context below.
  • $enclosure — An optional string input for this operation. Verify the allowed values in the examples and context below.
  • $escape — An optional string input for this operation. Verify the allowed values in the examples and context below.
  • $eol — An optional string input for this operation. Verify the allowed values in the examples and context below.

Declared return type: int|false. Distinguish false, an empty result, and exceptions where the API permits them.

Example 1: Basic Usage


This example demonstrates the main operation with a small, controlled input.

<?php

$streamHandle = fopen("php://temp", "w+");
fputcsv($streamHandle, ["name", "city"], ",", '"', '');
rewind($streamHandle);
echo stream_get_contents($streamHandle);
fclose($streamHandle);

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Example 2: Another Input


This example changes the input or surrounding setup to illustrate another behavior.

<?php

$streamHandle = fopen("php://temp", "w+");
fputcsv($streamHandle, ["Alex", "New York"], ",", '"', '');
rewind($streamHandle);
echo stream_get_contents($streamHandle);
fclose($streamHandle);

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Practical Example


This application-style example shows a self-contained usage pattern and checks its result.

<?php

$streamHandle = fopen("php://temp", "w+");
foreach ([["item", "price"], ["book", "12.00"]] as $csvRecord) { fputcsv($streamHandle, $csvRecord, ",", '"', ''); }
rewind($streamHandle);
echo stream_get_contents($streamHandle);
fclose($streamHandle);

The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.

Important Behavior and Edge Cases


  • Protect spreadsheet recipients against formula injection when exporting untrusted cells.
  • Use the intended input type and handle empty or malformed values explicitly.
  • Where this API requires an extension, version, or file, document that dependency before using it in a real application.

Comparison With Related Functions


Function or MethodWhen to Use It
fputcsvWrites a field array as one CSV record into a writable stream with quoting where needed.
fgetcsvReads one CSV record from an open stream, returning fields as an array or false at end of file.

Common Beginner Mistakes


MistakeCorrect Approach
Using this API without checking the required input or extensionProtect spreadsheet recipients against formula injection when exporting untrusted cells.
Splitting CSV on commasUse fgetcsv() or fputcsv(), which handle quoted delimiters.
Relying on the implicit escape defaultPass an explicit escape argument, preferably an empty string for RFC-style CSV.

Key Takeaways


  • Writes a field array as one CSV record into a writable stream with quoting where needed.
  • Protect spreadsheet recipients against formula injection when exporting untrusted cells.
  • Test both the expected case and at least one failure or empty-input case before deployment.

Writes a field array as one CSV record into a writable stream with quoting where needed. Use it when the operation fits your input, output and validation requirements.

Share this resourceSend this page to someone who may find it useful.