I searched for quite a while to see if it is possible to “add” to the file if you use ob_start with PHP.
I tried the following but did not work. Any way to achieve this?
<?php
$cacheFile = 'file.txt';
if ( (file_exists($cacheFile)) && ((fileatime($cacheFile) + 600) > time()) )
{
$content = file_get_contents($cacheFile);
echo $content;
} else
{
ob_start();
echo '<h1>Hello world</h1>';
$content = ob_get_contents();
ob_end_clean();
file_put_contents($cacheFile,$content,'a+');
echo $content;
}
?>
I took this example from another post on SO
source
share