有人建议将当前页面作为HTML文件保存到服务器的方法是什么?在这种情况下,还要注意安全性不是问题.
我花了无数个小时寻找这个,并没有找到一件事.
非常感谢您的帮助,谢谢!
编辑
谢谢大家的帮助,非常感谢.
HoL*_*ieR 64
如果您的意思是将页面的输出保存在文件中,则可以使用缓冲来执行此操作.您需要使用的函数是ob_start和ob_get_contents.
<?php
// Start the buffering //
ob_start();
?>
Your page content bla bla bla bla ...
<?php
echo '1';
// Get the content that is in the buffer and put it in your file //
file_put_contents('yourpage.html', ob_get_contents());
?>
Run Code Online (Sandbox Code Playgroud)
这将保存文件中页面的内容yourpage.html
.
我想我们可以使用PHP的输出控制功能,你可以先使用保存内容到变量然后将它们保存到新文件中,下次你可以测试它存在的html文件,然后渲染那个重新生成的页.
<?php
$cacheFile = 'cache.html';
if ( (file_exists($cacheFile)) && ((fileatime($cacheFile) + 600) > time()) )
{
$content = file_get_contents($cacheFile);
echo $content;
} else
{
ob_start();
// write content
echo '<h1>Hello world to cache</h1>';
$content = ob_get_contents();
ob_end_clean();
file_put_contents($cacheFile,$content);
echo $content;
}
?>
Run Code Online (Sandbox Code Playgroud)
示例取自:http://www.php.net/manual/en/function.ob-start.php#88212