Skip to content

Instantly share code, notes, and snippets.

@koconder
Last active January 14, 2019 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koconder/9806010 to your computer and use it in GitHub Desktop.
Save koconder/9806010 to your computer and use it in GitHub Desktop.
PHP Rules for CSV exports for UTF-8 and old browsers. Dynamic Exporting with rules to support each type of format which is extendable to call a parser on inputs.
<?php
//Do Download Headers if we have nothing else to show
if($uri_page[$no] == 'export'){
//Universal
ini_set('zlib.output_compression','Off');
if($uri_page[$no+1] == 'csv'){
//CSV
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename="adtrafik_export.'.$uri_page[$no+1].'"');
//IE FIX
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
}else{
header('Pragma: no-cache');
}
echo "\xEF\xBB\xBF"; // UTF-8 BOM
}else{
//JSON
header('Content-Type: ' . 'text/plain');
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename="adtrafik_export.'.$uri_page[$no+1].'"');
//IE FIX
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
}else{
header('Pragma: no-cache');
}
}//END IF TYPE CSV
}//END IF EXPORT
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment