Php Ob_刷新而不丢弃缓冲区
我有一个PHP脚本,需要几分钟来完成处理。当页面仍在加载时,我想在可用时显示部分PHP输出,这可以使用Php Ob_刷新而不丢弃缓冲区,php,html,codeigniter,Php,Html,Codeigniter,我有一个PHP脚本,需要几分钟来完成处理。当页面仍在加载时,我想在可用时显示部分PHP输出,这可以使用ob\u start()和ob\u flush()完成 在整个脚本完成执行之后,我想将所有PHP输出从一开始就保存到一个HTML文件中。这可以通过使用ob_start()和file_put_contents(“log.html”,ob_get_contents())来实现 问题:但是,由于我们正在调用ob\u flush(),因此使用file\u put\u contents()保存的最后一个文
ob\u start()
和ob\u flush()
完成
在整个脚本完成执行之后,我想将所有PHP输出从一开始就保存到一个HTML文件中。这可以通过使用ob_start()
和file_put_contents(“log.html”,ob_get_contents())来实现代码>
问题:但是,由于我们正在调用ob\u flush()
,因此使用file\u put\u contents()
保存的最后一个文件似乎被分为不同的文件。我怀疑这与调用file\u put\u contents()
之前调用ob\u start()
清除缓冲区有关,但为什么它不只是将最终ob\u flush()
和file\u put\u contents()
之间的输出保存到文件中,而是保存几个不同的文件?(我可能错了,单独的部分文件可能是由于脚本的部分执行)
换句话说,如何在长脚本执行时显示PHP输出,并将所有PHP输出保存到单个HTML文件中
PHP代码
// Start the buffering
ob_start();
......
ob_flush();
......
ob_flush();
......
file_put_contents("log.html", ob_get_contents());
您还可以使用ob\u get\u contents()
,将其保存到变量中,然后保存到文件和输出流中…您还可以使用ob\u get\u contents()
将其保存到变量中,然后保存到文件和输出流中…您可以获取缓冲区内容,并通过调用将其存储在变量中
您已经阅读了吗?您可以获取缓冲区内容,并通过调用将其存储在变量中
你读过吗?我能想到的两种方法:
保留一个变量(称为$content),并在每次调用ob_flush()时追加当前缓冲区:
使用fopen():
我可以想到以下几种方法:
保留一个变量(称为$content),并在每次调用ob_flush()时追加当前缓冲区:
使用fopen():
你为什么要做file_put_contents('log.html',$content.ob_get_contents())
而不是'file\u put\u contents'(log.html',$content);最终缓冲区中仍有一些内容。或者,如果您执行“file_put_contents('log.html',$content);”操作,您可以将“$content.=ob_get_contents();”放在前面的行上;为什么执行file_put_contents('log.html',$content.ob_get_contents())
而不是'file\u put\u contents'(log.html',$content);最终缓冲区中仍有一些内容。或者,如果执行“file\u put\u contents('log.html',$content);”操作,则可以将“$content.=ob\u get\u contents();”放在前面的行上
$content = '';
...
$content .= ob_get_contents();
ob_flush();
...
$content .= ob_get_contents();
ob_flush();
...
file_put_contents('log.html', $content . ob_get_contents());
ob_flush();
$fp = fopen('log.html', 'w+');
...
fwrite($fp, ob_get_contents());
ob_flush();
...
fwrite($fp, ob_get_contents());
ob_flush();
...
fwrite($fp, ob_get_contents());
fclose($fp);
ob_flush();