Skip to content

Instantly share code, notes, and snippets.

@aiiddqd
Created January 9, 2017 16:19
Show Gist options
  • Save aiiddqd/d7ebe02d5772e8d475038681557bb52e to your computer and use it in GitHub Desktop.
Save aiiddqd/d7ebe02d5772e8d475038681557bb52e to your computer and use it in GitHub Desktop.
Save array to CSV (for php and WordPress)
function save_data_to_file(){
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');
$data = array(
array('text1', 'text2'),
array('text1', 'text2')
);
foreach ($data as $item) {
fputcsv($output, $item);
}
fclose($output);
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment