I am creating a plugin in wordpress and i am trying to export data from array, it works. but the problem is with the output it print HTML also.
So please help me to get rid of this problem.
Here is my PHP code that i try
public function export_data()
{
ob_start();
$filename = "export_data" . rand(1, 100) . ".csv";
$export_data = $this->order_detail_items();
ob_end_flush();
$output = fopen("php://output", 'w') or die("Can't open php://output");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename={$filename}");
header('Expires: 0');
header('Pragma: public');
fputcsv($output, array('Store Name', 'Product name', 'Sell Quantity', 'Date', '(£) Total Price'));
foreach ($export_data as $key => $data) {
fputcsv($output, $data);
}
fclose($output);
die();
}
Any solution appreciated!