<?php
/**
* Takes an array of data and creates a csv file that will automatically download
*/
function downloadable_csv( $data, $filename = 'data_csv' ){
/*
Sample Data Format:
array(
'headings' => array(),
'data' => array(
array()
)
);
*/
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$filename.'.csv');
// Create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// Output the column headings
fputcsv($output, $data['headings']);
// Loop over the rows, outputting them
foreach( $data['data'] as $row ){
fputcsv($output, $row, ',', '"');
}
}