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));
}
}
}
Please any of the files like index.php of the framework or any other file having empty lines in the beginning, this can cause empty lines in the csv.
Please check your other files which are included before the function call and which have print space or etc. You can check that using notepad open the exported csv file in notepad you can see if there is space or not your php code is working perfect there is no any issue with that. I have tested with bellow array its working fine.
$data = array(
'0' => array('ip address' => '1.3.2.1', 'ip state' => 'Active'),
'1' => array('ip address' => '1.3.2.2', 'ip state' => 'Reserved'),
'2' => array('ip address' => '1.3.2.3', 'ip state' => 'Active'),
'3' => array('ip address' => '1.3.2.4', 'ip state' => 'Active')
);
switch ("export-to-csv")
{
case "export-to-csv" :
$filename = "1.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);
exit();
default :
die("Unknown action : " . $_POST["action"]);
break;
}
function ExportCSVFile($records)
{
$fh = fopen('php://output', 'w');
$heading = false;
if (!empty($records))
{
foreach ($records as $row)
{
if (!$heading)
{
fputcsv($fh, array_keys($row));
$heading = true;
}
fputcsv($fh, array_values($row));
}
}
}