abhi_madhani

Convert PHP array to csv (without saving to file)

Jun 7th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.47 KB | None | 0 0
  1. <?php
  2.  
  3.     $data = array();
  4.     $stream = fopen("php://temp", 'w');
  5.     foreach($data as $key => $val) {
  6.         fputcsv($stream, $val);
  7.     }
  8.     rewind($stream);
  9.     $data = stream_get_contents($stream);
  10.     fclose($stream);
  11.  
  12.     header('Content-Description: File Transfer');
  13.     header('Content-Type: text/csv');
  14.     header('Content-Disposition: attachment; filename=data.csv');
  15.     header('Expires: 0');
  16.     header('Cache-Control: must-revalidate');
  17.     header('Pragma: public');
  18.    
  19.     echo $data;
  20.  
  21. ?>
Advertisement
Add Comment
Please, Sign In to add comment