Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Created February 20, 2012 06:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanakahisateru/1868071 to your computer and use it in GitHub Desktop.
Save tanakahisateru/1868071 to your computer and use it in GitHub Desktop.
CSV via HTTP response by PHP
<?php
ob_start();
header('Content-Type: text/plain;charset=UTF-8');
$stream = fopen('php://output', 'w');
fputcsv($stream, array(1, 2, 3));
fputcsv($stream, array(4, 5, 6));
// more complex codes would be here. It may cause error.
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=output.csv");
echo mb_convert_encoding(ob_get_clean(), 'SJIS', 'UTF-8');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment