Skip to content

Instantly share code, notes, and snippets.

@markjames
Created June 15, 2010 10:26
Show Gist options
  • Save markjames/438954 to your computer and use it in GitHub Desktop.
Save markjames/438954 to your computer and use it in GitHub Desktop.
<?php
ob_start();
$out = fopen('php://output', 'w');
$headerRow = array();
foreach( array_keys($result[0]) as $fieldname ) {
$headerRow []= isset($fields[$fieldname]) ? $fields[$fieldname]['title'] : $fieldname;
}
fputcsv($out, $headerRow );
foreach( $result as $row ) {
fputcsv($out, $row);
}
$contents = ob_get_contents();
ob_end_clean();
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
header('Content-Type: text/csv');
header('Content-Disposition: inline; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Length: ".strlen($contents));
} else {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".strlen($contents));
}
fclose($out);
echo $contents;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment