Please follow the below steps for creating csv file in php using fopen method

  //For CSV report
  $fp = fopen($_SERVER['DOCUMENT_ROOT'].'/csv/myfirstfile.csv', 'w');  //set file path
  $headings = array('Heading col-1','Heading col-2','Heading col-3','Heading col-4');   //For csv header row
  $row_array = array (
      array("row-1-col-1-data", "row-1-col-2-data" ,"row-1-col-3-data"),
      array("row-2-col-1-data", "row-2-col-2-data", "row-2-col-3-data")
  );
  fputcsv($fp,$headings);
 
  foreach ( $book_csv['row'] as $line ) {
     fputcsv($fp, $line);
  }
 
  fclose($fp);

0 Comments

Leave a Comment