I have looked for quite a while now, to see if it’s possible to “append” to a file if using ob_start with PHP.
I have tried the following but did not work. Any way of achieving this?
<?php
$cacheFile = 'file.txt';
if ( (file_exists($cacheFile)) && ((fileatime($cacheFile) + 600) > time()) )
{
$content = file_get_contents($cacheFile);
echo $content;
} else
{
ob_start();
// write content
echo '<h1>Hello world</h1>';
$content = ob_get_contents();
ob_end_clean();
file_put_contents($cacheFile,$content,'a+'); // I added the a+
echo $content;
}
?>
I borrowed the above example from another post on S.O.
file_put_contentsdoesn’t work that way. To append, you need to usefopen,fwriteandfclosemanually.