I wrote below code in a controller action.
$response = new StreamedResponse();
$i = -999999;
$response->setCallback(function () {
while($i < 999999){
echo '{"G1ello":"hualala"}';
$i = $i + 1;
}
});
$filename = "Data.json";
$contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Content-Disposition', $contentDisposition);
return $response;
This way I was able to download 1.7 GB JSON file.
On the other hand I created a 700 MB file and tried to get its content using code
file_get_contents($file)
error was thrown.
Allowed memory size of XXX bytes exhausted (tried to allocate YYY bytes)
I am not sure how StreamedResponse and setCallback function worked here. Can someone explain?
The problem is cleary not about the StreamResponse but about the fact that you're trying to read the 700MB file to memory at one (Before returning in by small parts).
Depending on the file you read, you should read it by chunks:
$maxlen and $offset to limit what you read each time.And you will have to do this is a loop, that will update the StreamResponse with what you have read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With