I have the below data in an array and want to write to CSV file. Everything is working except i see first two empty lines in the .csv file
Any help is appreciated... Thanks again!!!!
Output in CSV file looks like
empty Line1
empty Line2
ip address ip state description hostmane mac owner device notes
data......
Array data:
Array (
[0] =>Array ( [ip address] => 1.3.2.1 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => )
[1] =>Array ( [ip address] => 1.3.2.2 [ip state] => Reserved [description] => [hostname] => linux [mac] => [owner] => test [device] => Linux Server [note] => Linux123 )
[2] => Array ( [ip address] => 1.3.2.3 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => )
[3] => Array ( [ip address] => 1.3.2.4 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => )
)
PHP Function i am using to write to CSV file is:
switch("export-to-csv")
{
case "export-to-csv" :
// Submission from
$filename = $_GET["exformat"] . ".csv";
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: 0");
ExportCSVFile($data);
//$_GET["exformat"] = '';
exit();
default :
die("Unknown action : ".$_POST["action"]);
break;
}
function ExportCSVFile($records) {
// create a file pointer connected to the output stream
#print_r ($records); return;
$fh = fopen( 'php://output', 'w' );
$heading = false;
if(!empty($records)) {
foreach($records as $row) {
if(!$heading) {
// output the column headings
fputcsv($fh, array_keys($row));
$heading = true;
}
// loop over the rows, outputting them
fputcsv($fh, array_values($row));
}
}
}