Uncategorized

Create CSV file using php code

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);

Advertisement

Did you find this helpful? Share it!

Leave a Comment

Your email address will not be published. Required fields are marked