流输出到文件和浏览器 [英] Streaming output to a file and the browser

查看:143
本文介绍了流输出到文件和浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在寻找比这更有效的东西:

So, I'm looking for something more efficient than this:

<?php
ob_start();
include 'test.php';
$content = ob_get_contents();

file_put_contents('test.html', $content);

echo $content;
?>

上述问题:


  • 在整个页面呈现之前,客户不会收到任何东西

  • 文件可能很大,所以我宁愿不把整个东西都放在内存中
  • >
  • Client doesn't receive anything until the entire page is rendered
  • File might be enormous, so I'd rather not have the whole thing in memory

有什么建议吗?

推荐答案

有趣的问题;

我认为您需要第二个请求,从您的前端PHP脚本转到您的服务器。这可能是对 http://localhost/test.php 的简单调用。如果使用fopen-wrappers,则可以使用fread()在呈现test.php的输出时提取其输出,并在接收到每个块后,将其输出到屏幕并将其附加到test.html文件中。

I'm thinking you'll need to have a second request going from your front-facing PHP script to your server. This could be a simple call to http://localhost/test.php. If you use fopen-wrappers, you could use fread() to pull the output of test.php as it is rendered, and after each chunk is received, output it to the screen and append it to your test.html file.

这是看起来(未经测试!):

Here's how that might look (untested!):

<?php
$remote_fp = fopen("http://localhost/test.php", "r");
$local_fp = fopen("test.html", "w");
while ($buf = fread($remote_fp, 1024)) {
    echo $buf;
    fwrite($local_fp, $buf);
}
fclose($remote_fp);
fclose($local_fp);
?>

这篇关于流输出到文件和浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆