php - How does StreamedResponse work? -
i wrote below code in 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 able download 1.7 gb json file.
on other hand created 700 mb file , tried content using code
file_get_contents($file)
error thrown.
allowed memory size of xxx bytes exhausted (tried allocate yyy bytes)
i not sure how streamedresponse , setcallback function worked here. can explain?
the problem cleary not streamresponse
fact you're trying read 700mb file memory @ 1 (before returning in small parts).
depending on file read, should read chunks:
- if text file, read line line example
- if binary file, use same function, use
$maxlen
,$offset
limit read each time.
and have loop, update streamresponse
have read.
Comments
Post a Comment