Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created December 6, 2023 13:56
Show Gist options
  • Save tsh-code/e6d22cc3b178de3b9513bd1f17304ee8 to your computer and use it in GitHub Desktop.
Save tsh-code/e6d22cc3b178de3b9513bd1f17304ee8 to your computer and use it in GitHub Desktop.
<?php
class Generator {
public function generateJsonFile(array $data): void;
{
$content = json_encode($data);
header(‘Content-Type: application/json`);
header(‘Content-Disposition: attachment; filename="data.json”`);
ob_start();
echo $content;
ob_end_flush();
exit;
}
public function generateCsvFile(array $data): void
{
$stream = fopen(‘php://memory’);
foreach ($data as $line) {
fputcsv($stream, $data, ‘;’);
}
$content = ‘’;
while (false === feof($stream)) {
$content .= fread($stream, 1024);
}
fclose($stream);
header(‘Content-Type: text/csv`);
header(‘Content-Disposition: attachment; filename="data.csv”`);
ob_start();
echo $content;
ob_end_flush();
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment