Created
December 6, 2023 13:56
-
-
Save tsh-code/e6d22cc3b178de3b9513bd1f17304ee8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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