如何运行一个很长的 PHP 脚本并通过 HTTP 不断向浏览器发送更新?
如何运行一个很长的 PHP 脚本并通过 HTTP 不断向浏览器发送更新?
与输出缓冲有关,但我不知道具体如何。
How do you run a long PHP script and keep sending updates to the browser via HTTP?
Something to do with output buffering but I don't know exactly how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
输出缓冲正在朝着正确的方向思考,您可以使用
ob_start()
开始输出缓冲,就像在脚本顶部的某个位置(在任何会话之前)使用会话 (session_start
) 一样。输出已发送。然后,您可以使用ob_flush和flush来保持刷新输出。例如,如果您处于
foreach
循环中,并且在每个循环结束时您想要输出新行并等待 1 秒,您就可以这样做。但还要查看
set_time_limit
,否则人们可能会在 30 秒左右后遇到超时。另一个快速说明是,某些浏览器在实际开始显示输出之前需要最小数量的输出字节。我不确定它有多少字节,我认为大约是 4000。此外,某些浏览器在关闭之前不会渲染某些元素(例如表格)。所以冲洗在那里也不起作用。
Output Buffering is thinking in the right direction, you start output buffering with
ob_start()
just like you would with sessions (session_start
) somewhere in the top of your script, before any output is sent.Then, you can use
ob_flush
andflush
to keep flushing the output. For example, if you are in aforeach
loop and at the end of each loop you want to output the new row and wait 1 second you would can do that.But also look at
set_time_limit
, because otherwise people might experience a timeout after 30 seconds or so.Another quick note, some browsers require a minimum number of bytes of output before they actually start showing it. I'm not sure what amound of bytes it was, I think it was around the 4000. Also, some browsers won't render certain elements (like tables) until they are closed. So flushing won't work there either.
这看起来像您所追求的:
输出缓冲
This looks like what you are after:
Output Buffering
您还可以有一种后台任务,以及一个为您提供进度的界面。
例如,一个名为 job.php
和progress.php 的
页面然后一些ajax 调用progress.php?task=mytaskid 并更新GUI。我见过这种用于“大”文件上传的方法,并发现它很棒。
编辑:抱歉,这并不能完全回答最初的问题。
You can also have a kind of background task, and an interface giving you the progress rate.
for instance, a page called job.php
and progress.php
Then some ajax calls to progress.php?task=mytaskid and update the GUI. I have seen such method for a "big" file upload and found it great.
Edit: sorry, this doesn't exactly respond the initial question.
我使用简单的 HTTP 输出更新页面,以使其正常工作:
中的所有打开元素 - 否则它不会显示
)
这是我的代码:
I got it the page updating using simple HTTP outputting, to make it work:
<body>
- else it won't display<p>
)Here is my code: