Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 0.63 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to alter headers to download a file in WordPress when headers are already being sent out in a previous file?
  2. $tmp_handle = fopen('php://memory', 'r+');
  3.  
  4. foreach ($arr as $fields) {
  5.     fputcsv($tmp_handle, $fields);
  6. }
  7.  
  8. header('Content-type: text/csv');
  9. header('Content-disposition: attachment;filename=MyVerySpecial.csv');
  10. rewind($tmp_handle);
  11.  
  12. echo stream_get_contents($tmp_handle);
  13.        
  14. add_action('template_redirect', 'download_page');
  15. function download_page() {
  16.     if($_GET['download'] == 'true') {
  17.         //Output header information and the file content
  18.         exit; // Stops Wordpress from executing anything else.
  19.     }
  20. }