PHP Export CSV made simple

Export data to an CSV file and output to browser

You can export your data to a CSV file that will automatically ooutput in a browser (Tested in Exploer, Firefox and Chrome)

 

$sep=';';
$eol="\n"; // Double quotes here
$file="export";

//Redirect output to a client’s web browser (CSV)
header('Content-Type: application/csv-tab-delimited-table');                 
header('Content-Disposition: attachment;filename="'.$file.'.csv"');
header('Cache-Control: max-age=0');
    
echo "a".$sep."b".$sep."c".$eol;
echo "d".$sep."e".$sep."f".$eol;
echo "g".$sep."h".$sep."i".$eol;
exit();

 

The double quotes are very ilmportant  for the new line character, as the \n isn't parsed for single quotes but it is for double quotes.