• PHP Video Tutorials

PHP - Function fputcsv()



The fputcsv() function can format a line as CSV and writes it to an open file. This function can return the length of the written string or false on failure.

Syntax

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

Example

<?php
   $list = array (
      array('aaa', 'bbb', 'ccc', 'dddd'),
      array('123', '456', '789'),
      array('"aaa"', '"bbb"')   
   );

   $fp = fopen("/PhpProject/sample.csv", "w");

   foreach($list as $fields) {
      fputcsv($fp, $fields);
   }
   echo "The list data has been inserted successfully to .csv file";

   fclose($fp);
?>

Output

The list data has been inserted successfully to .csv file
php_function_reference.htm
Advertisements