
Untitled
By: a guest on
May 4th, 2012 | syntax:
None | size: 0.63 KB | hits: 12 | expires: Never
How to alter headers to download a file in WordPress when headers are already being sent out in a previous file?
$tmp_handle = fopen('php://memory', 'r+');
foreach ($arr as $fields) {
fputcsv($tmp_handle, $fields);
}
header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv');
rewind($tmp_handle);
echo stream_get_contents($tmp_handle);
add_action('template_redirect', 'download_page');
function download_page() {
if($_GET['download'] == 'true') {
//Output header information and the file content
exit; // Stops Wordpress from executing anything else.
}
}