将当前页面保存为HTML到服务器

38 html php file save

有人建议将当前页面作为HTML文件保存到服务器的方法是什么?在这种情况下,还要注意安全性不是问题.

我花了无数个小时寻找这个,并没有找到一件事.

非常感谢您的帮助,谢谢!

编辑

谢谢大家的帮助,非常感谢.

HoL*_*ieR 64

如果您的意思是将页面的输出保存在文件中,则可以使用缓冲来执行此操作.您需要使用的函数是ob_startob_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.

  • 非常感谢.你刚救了我的命. (2认同)
  • @Walahh除非调用`chdir`,否则它应该位于所请求脚本的同一文件夹中.如果你不确定它是什么,你可以调用`getcwd`. (2认同)

Che*_*rma 9

我想我们可以使用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