【发布时间】:2012-06-09 19:13:43
【问题描述】:
我已经找了很长时间了,看看如果在 PHP 中使用 ob_start 是否可以“附加”到一个文件。
我尝试了以下方法,但没有成功。有什么方法可以实现吗?
<?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;
}
?>
我从 S.O. 的另一篇文章中借用了上面的示例
【问题讨论】: