PHP fputcsv
is a simple resolution for changing an array to CSV and including it to a file.
It’s a basic instance to show the ability of PHP. A perform that does a job usually delegated to an exterior library in different languages. One other related instance is the mail() PHP perform.
fputcsv syntax
PHP fputcsv
kind a comma-separated line from an array and put it right into a CSV file. The beneath syntax reveals the arguments wanted for this perform.
fputcsv(
useful resource $stream,
array $fields,
string $separator = ",",
string $enclosure = """,
string $escape = "",
string $eol = "n"
): int|false
Description
The PHP fputcsv()
perform wants a goal file stream and a single-dimensional array for that. It’s in PHP from model 5.1.0. In PHP 7.4 and eight.1 it has important adjustments to complement the perform with extra parameters and options.
Enabling auto_detect_line_endings adjustments the habits of the PHP fputcsv() perform. It reduces the failure charge on detecting line endings of a CSV file. This can be useful if you’re studying or writing with a CSV created by a Macintosh laptop.
Parameters
$stream
– It’s the opened file pointer as a goal to put in writing the CSV line.
$fields
– An array of fields to kind the CSV line.
$separator
– It’s optionally available and it’s a delimiter between two CSV components.
$enclosure
– It’s to surround the CSV column knowledge. Not all of the column knowledge is enclosed by this parameter. If the CSV content material comprises area or any escape sequences as part of it, then it will likely be enclosed by the $enclosure.
$escape
– It’s a previous character that stops knowledge truncation because of an prevalence of a particular character. If the info comprises a particular character utilized in fputcsv parameters $separator, $enclosure, then it must be escaped.
$eol
– That is for customizing the end-of-line for every CSV line.
Return values
It returns the size of the written CSV string. If something goes mistaken then it returns false on failure.
PHP fputcsv instance
This program is to kind CSV from the enter array and write it to a CSV file.
$fieldArray has an array for the CSV line knowledge. The PHP foreach iterates the sector array and creates CSV line knowledge utilizing fputcsv() perform.
<?php
$fieldArray = array(
array('Mechanical toys', 'Battery toys', 'Distant management toys'),
array('Mechanical automobiles', 'Battery automobiles', 'Distant automobiles')
);
$filePointer = fopen('output-csv-file.csv', 'w');
foreach ($fieldArray as $fields) {
fputcsv($filePointer, $fields);
}
fclose($filePointer);
?>
Instance output
This code saves the output CSV file to the folder from the place it’s operating. The output CSV will include the next comma-separated, enclosed knowledge in every line.
output-csv-file.csv
"Mechanical toys","Battery toys","Distant management toys"
"Mechanical automobiles","Battery automobiles","Distant automobiles"
Tips on how to present CSV to the browser?
As a substitute of writing to an output file, this instance will show 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);
?>
Instance output
CSV,PDF,XLSX
.csv,.pdf,.xlsx