Skip to content

Instantly share code, notes, and snippets.

@shalaby
Created March 4, 2014 20:05
Show Gist options
  • Save shalaby/9354533 to your computer and use it in GitHub Desktop.
Save shalaby/9354533 to your computer and use it in GitHub Desktop.
Generate CSV file from a PHP array
function generateCsv($data, $delimiter = ',', $enclosure = '"') {
$handle = fopen('php://temp', 'r+');
foreach ($data as $line) {
fputcsv($handle, $line, $delimiter, $enclosure);
}
rewind($handle);
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
return $contents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment