PHP fputcsv() Function

The fputcsv() function formats a line as CSV and writes it to an open file.This function returns the length of the written string, or FALSE on failure.

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\\" ]]] )

fputcsv() formats a line (passed as a fields array) as CSV and write it (terminated by a newline) to the specified file handle.

Example -

The above example will write the following to file.csv:

aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""

ParameterDescription
fileRequired. Specifies the open file to write to
fieldsRequired. Specifies which array to get the data from
separatorOptional. A character that specifies the field separator. Default is comma ( , )
enclosureOptional. A character that specifies the field enclosure character. Default is "