Skip to content

Instantly share code, notes, and snippets.

@pkdavies
Last active September 27, 2015 01:17
Show Gist options
  • Save pkdavies/1188847 to your computer and use it in GitHub Desktop.
Save pkdavies/1188847 to your computer and use it in GitHub Desktop.
Correct cross-browser force CSV
<?php
$filename = "my-csv-file-".date("Y-m-d").".csv";
ob_start( ini_get('zlib.output_compression')? 'ob_gzhandler' : '' );
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: public");
header("Cache-Control: max-age=0");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
$agent = trim($_SERVER['HTTP_USER_AGENT']);
if ((preg_match('|MSIE ([0-9.]+)|', $agent, $version)) || (preg_match('|Internet Explorer/([0-9.]+)|', $agent, $version))) {
header('Content-Type: application/force-download');
if ($version == '5.5') {
header('Content-Disposition: filename="' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
} else {
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: inline; filename="' . $filename.'"');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment