In a wordpress woocommerce installation I need to export some custom order fields to a csv file. Everything works fine, only I don't get the right content in the downloaded file.
Here is my code:
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.$filename.'"');
$output = fopen( get_temp_dir() . $filename, 'w');
foreach ($array as $line) {
fputcsv( $output, $line );
}
fclose($output);
}
The array is something like
$array = array(
array('Head1', 'Head2', 'Head3', 'Head4'),
array('Data1', 'Data2', 'Data3', 'Data4'),
array('Data5', 'Data6', 'Data7', 'Data8')
);
The function is located on a custom admin page, and is triggered by a submit button.
When I press the button, a csv file is generated and written to the temp folder, named as $filename. That file works fine.
The file, which is downloaded automatically, forced by the second header entry in the function, is named correctly ($filename), but the content is the source code of that custom admin page.
I don't need that file to be stored in the temp folder, it's only for now until the automatic download works. I tried to put 'php://temp' or 'php://output' in the fopen function, but that didn't change the content of the download file.
What am I missing?