The fputcsv() function formats a line as CSV and writes it to an open file. The function returns the length of the written string.
Syntax
fputcsv(file_pointer, fields, delimiter, enclosure, escape)
Parameters
file_pointer − A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen().
fields − Array of string.
delimiter − Character that specifies the field separator. Default is comma ( , )
enclosure − Set the field enclosure character. Defaults as a double quotation mark.
escape − Set the escape character. Defaults as a backslash (\).
Return
The fputcsv() function returns the length of the written string.
The following is an example that writes content to the “employees.csv” file.
Example
<?php $mylist = array ( "Jack,Tim", "Henry,Tom", ); $file_pointer = fopen("employees.csv","w"); foreach ($mylist as $line) { fputcsv($file_pointer,explode(',',$line)); } fclose($file_pointer); ?>
The CSV file “employees.csv” will have the following content now.
Output
Jack,Tim, Henry,Tom