Advertisement
Guest User

Untitled

a guest
Jun 30th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1.  
  2. // Headings and rows
  3. $headings = array('ID', 'Name', 'Colour');
  4. $array = array(
  5. array(1, 'Apple', 'Green'),
  6. array(2, 'Banana', 'Yellow'),
  7. array(3, 'Orange', 'Orange'),
  8. );
  9.  
  10. // Open the output stream
  11. $fh = fopen('php://output', 'w');
  12. // Start output buffering (to capture stream contents)
  13. ob_start();
  14. fputcsv($fh, $headings);
  15. // Loop over the * to export
  16. if (! empty($array)) {
  17. foreach ($array as $item) {
  18. fputcsv($fh, $item);
  19. }
  20. }
  21. // Get the contents of the output buffer
  22. $string = ob_get_clean();
  23. $filename = 'csv_' . date('Ymd') .'_' . date('His');
  24. // Output CSV-specific headers
  25. header("Pragma: public");
  26. header("Expires: 0");
  27. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  28. header("Cache-Control: private",false);
  29. header("Content-Type: application/octet-stream");
  30. header("Content-Disposition: attachment filename=\"$filename.csv\";" );
  31. header("Content-Transfer-Encoding: binary");
  32.  
  33.  
  34. exit($string);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement