PHP | fputcsv( ) Function
The fputcsv() function in PHP is an inbuilt function which is used to format a line as CSV(comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the length of the written string on success or FALSE on failure.
Syntax:
int fputcsv ( $file, $fields, $separator, $enclosure )
Parameters: The fputcsv() function in PHP accepts four parameters as explained below.
- $file: It is a mandatory parameter which specifies the file.
- $fields: It is a mandatory parameter which specifies which array to get data from.
- $separator: It is an optional parameter which specifies the field separator. By default the fputcsv() function uses comma.
- $enclosure: It is an optional parameter which specifies the field enclosure character. By default the fputcsv() function uses.
Return Value: This function returns the length of the written string on success or FALSE on failure.
Exceptions:
- If an enclosure character is contained in a field, it will be escaped by doubling it, unless it is immediately preceded by an escape_char.
- Enabling the auto_detect_line_endings run-time configuration option may help resolve the problem of PHP properly recognizing the line endings when reading files either on or created by a Macintosh computer.
Below programs illustrate the fputcsv() function:
Program 1:
<?php // Sample data for formatting in CSV format $employees = array("Raj, Singh, Developer, Mumbai", "Sameer, Pandey, Tester, Bangalore", "Raghav, Chauhan, Manager, Delhi"); // opening the file "data.csv" for writing $myfile = fopen("gfg.csv", "w"); // formatting each row of data in CSV format // and outputting it foreach ($employees as $line) { fputcsv($myfile, explode(',',$line)); } // closing the file fclose($myfile); ?> |
Output:
Raj, Singh, Developer, Mumbai Sameer, Pandey, Tester, Bangalore Raghav, Chauhan, Manager, Delhi
Program 2:
<?php // Sample data for formatting in CSV format $random_data = array( array("abc, efg, jhi, klm"), array("123, 456, 789"), array("11aa, 22bb, 33cc, 44dd") ); // opening the file "data.csv" for writing $myfile = fopen("gfg.csv", "w"); // formatting each row of data in CSV format // and outputting it foreach ($random_data as $line) { fputcsv($myfile, $line); } // closing the file fclose($myfile); ?> |
Output:
abc, efg, jhi, klm 123, 456, 789 11aa, 22bb, 33cc, 44dd
Reference: http://php.net/manual/en/function.fputcsv.php
Recommended Posts:
- PHP | SplFileObject fputcsv() Function
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- PHP | abs() Function
- CSS | url() Function
- PHP | next() Function
- D3.js | d3.mean() function
- D3.js | d3.sum() function
- p5.js | nfs() Function
- p5.js | nfp() Function
- p5.js | nfc() function
- p5.js | hue() function
- D3.js | d3.map.set() Function
- PHP | exp() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



