Send Data as CSV File


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function sendCsv($data, $filename) {
  4. $fp = fopen('php://output', 'w');
  5. foreach ($data as $r) {
  6. fputcsv($fp, $r);
  7. }
  8. $contents = ob_get_clean();
  9. fclose($fp);
  10. header('Content-type: application/octet-stream');
  11. header("Content-Disposition: attachment; filename=\"$filename\"");
  12. echo $contents;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.