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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -