PHP fputcsv

by Vincy. Last modified on April 10th, 2024.

PHP fputcsv is an easy solution for converting an array to CSV and adding it to a file.

It is a classic example to demonstrate the power of PHP. A function that does a job generally delegated to an external library in other languages. Another similar example is the mail() PHP function.

fputcsv syntax

PHP fputcsv form a comma-separated line from an array and put it into a CSV file. The below syntax shows the arguments needed for this function.

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

Description

The PHP fputcsv() function needs a target file stream and a single-dimensional array for that. It is in PHP from version 5.1.0. In PHP 7.4 and 8.1 it has significant changes to enrich the function with additional parameters and features.

Enabling auto_detect_line_endings changes the behavior of the PHP fputcsv() function. It reduces the failure rate on detecting line endings of a CSV file. This will be helpful if you are reading or writing with a CSV created by a Macintosh computer.

Parameters

$stream – It is the opened file pointer as a target to write the CSV line.

$fields – An array of fields to form the CSV line.

$separator – It is optional and it is a delimiter between two CSV elements.

$enclosure – It is to enclose the CSV column data. Not all the column data is enclosed by this parameter. If the CSV content contains space or any escape sequences as a part of it, then it will be enclosed by the $enclosure.

$escape – It is a preceding character that prevents data truncation due to an occurrence of a special character. If the data contains a special character used in fputcsv parameters $separator, $enclosure, then it should be escaped.

$eol – This is for customizing the end-of-line for each CSV line.

Return values

It returns the length of the written CSV string. If anything goes wrong then it returns false on failure.

PHP fputcsv example

This program is to form CSV from the input array and write it to a CSV file.

$fieldArray has an array for the CSV line data. The PHP foreach iterates the field array and creates CSV line data using fputcsv() function.

PHP fputcsv

<?php
$fieldArray = array(
    array('Mechanical toys', 'Battery toys', 'Remote control toys'),
    array('Mechanical cars', 'Battery cars', 'Remote cars')
);
$filePointer = fopen('output-csv-file.csv', 'w');
foreach ($fieldArray as $fields) {
    fputcsv($filePointer, $fields);
}
fclose($filePointer);
?>

Example output

This code saves the output CSV file to the folder from where it is running. The output CSV will contain the following comma-separated, enclosed data in each line.

output-csv-file.csv

"Mechanical toys","Battery toys","Remote control toys"
"Mechanical cars","Battery cars","Remote cars"

How to show CSV to the browser?

Instead of writing to an output file, this example will display the resultant CSV to the browser. The PHP fputcsv() writes the CSV line to the output stream.

<?php
$outputStream = fopen('php://output', 'w');
fputcsv($outputStream, array('CSV', 'PDF', 'XLSX'));
fputcsv($outputStream, array('.csv', '.pdf', '.xlsx'));
fclose($outputStream);
?>

Example output

CSV,PDF,XLSX
.csv,.pdf,.xlsx
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page