Memory allocation problems while using a Stream through PHP -
i'm using stream in php using guzzlehttp\stream\stream class. whilst using i'm getting php memory allocation problems. there method can use doesn't use memory?
problem
when need serve content-range of 0-381855148 bytes (for example) causes me memory allocation issues. there method how can serve content while not needing memory? passes data straight through, instead of "reserving" in memory?
this part of code responsible error...
$stream = guzzlehttp\stream\stream::factory(fopen($path, 'r')); $stream->seek($offset); while (!$stream->eof()) { echo $stream->read($length); } $stream->close(); this passed callback function stream.
background
first tried fixing problem providing maximum chunk length stream. did giving stream maximum offset. it's fixes memory allocation problem, new problems arise in firefox when distributing dynamic video content. chrome doesn't have problems it.
it's because firefox asks "0-" content-range give content-range "0-" back. instead need give whole range (until maximum) causes infamous "allowed memory size of 262144 bytes exhausted (tried allocate 576 bytes)" error.
disclaimer: it's little bit more technical. wanted keep simple.
does knows solution? thanks.
found answer on different forum.
the reason memory exhausted problem because of guzzle , way build (psr-7).
a more in in depth article problem: https://evertpot.com/psr-7-issues/
i fixed using code:
if ($i = ob_get_level()) { # clear buffering: while ($i-- && ob_end_clean()); if (!ob_get_level()) header('content-encoding: '); } ob_implicit_flush(); $fp = fopen($path, 'rb'); fseek($fp, $offset); while ($length && !feof($fp)) { $chunk = min(8192, $length); echo fread($fp, $chunk); $length -= $chunk; } fclose($fp); credits goes djmaze
Comments
Post a Comment