I have a script that echo out content in a php script and resulting in a very large file, e.g. 100MB
Currently I use the following way to capture the output and write to another file
ob_start();
require_once 'dynamic_data.php'; // echo 100MB data
$data = ob_get_clean();
file_put_contents($path, $data);
Are there any easy way to re-write the above program (better not touching dynamic_data.php as it is hard to re-factor) so it can stream the output to the file directly without saving the content in the memory?
The
ob_startdocumentation provides a workaround for this. You need to pass in a$output_callbackand a$chunk_size.Say you set
$chunk_sizeto 1MB. Then every 1MB of buffered output data, your$output_callbackwill be called with this data and you can flush it to disk (meanwhile the output buffer is implicitly flushed).