使用 PHP 和 Apache 防止输出缓冲 [英] Prevent output buffering with PHP and Apache

查看:28
本文介绍了使用 PHP 和 Apache 防止输出缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送大量记录的 PHP 脚本,我想在每条记录可用时立即刷新:客户端能够在每条记录到达时对其进行处理,它不需要等待整个响应.我意识到整个传输需要稍长的时间,因为它需要在多个数据包中发送,但它仍然允许客户端更快地开始工作.

I have a PHP script which sends a large number of records, and I want to flush each record as soon as it is available: the client is able to process each record as it arrives, it does not need to wait for the entire response. I realize it takes slightly longer for the entire transfer because it needs to be sent in multiple packets, but it still allows the client to start working sooner.

我尝试了所有不同的 flush()ob_flush() 函数,但似乎没有任何帮助在页面完成之前通过线路实际发送数据.我已经确认它不是网络浏览器,因为我已经使用 telnet 对其进行了测试.

I've tried all the different flush() and ob_flush() functions but nothing seems to help get the data actually sent over the line before the page is finished. I've confirmed that it is not the web browser because I've tested it using telnet.

推荐答案

唯一对我有用的解决方案是将 php.ini 中的 output_buffering 指令设置为关闭".我不想为整个服务器执行此操作,只想为这一特定资源执行此操作.通常,您可以使用 PHP 脚本中的 ini_set,但无论出于何种原因,php 都不允许以这种方式设置 output_buffering(请参阅 php 手册).

The only solution that worked for me was to set the output_buffering directive in php.ini to "Off". I didn't want to do this for the entire server, just this one specific resource. Normally you could use ini_set from the PHP script, but for whatever reason php doesn't allow output_buffering to be set in this way (see the php manual).

事实证明,如果您使用的是 Apache,您可以从您的服务器配置中设置一些 php ini 指令(包括 output_buffering),包括一个 .htaccess 文件.所以我在 .htaccess 文件中使用了以下内容来禁用该文件的 output_buffering:

Well it turns out that if you're using Apache, you can set some php ini directives (including output_buffering) from your server config, including a .htaccess file. So I used the following in a .htaccess file to disable the output_buffering just for that one file:

<Files "q.php">
    php_value output_buffering Off
</Files>

然后在我的静态服务器配置中,我只需要 AllowOverride Options=php_value(或更大的锤子,如 AllowOverride All)才能允许它.htaccess 文件.

And then in my static server configuration, I just needed AllowOverride Options=php_value (or a larger hammer, like AllowOverride All) in order for that to be allowed in a .htaccess file.

这篇关于使用 PHP 和 Apache 防止输出缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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