如何在 PHP 中禁用输出缓冲
我编写了一个简单的中继脚本,它连接到网络摄像头并从套接字读取数据,并使用打印功能输出此数据。数据是已设置边界的 MJPG 数据。我只是输出读取到的数据。
问题是 PHP 似乎正在缓冲这些数据。当我将相机设置为 1 FPS 时,画面会冻结 7-8 秒,然后快速显示 8 帧。如果我将分辨率设置为很大,相机会以每秒或多或少 1 帧的速度移动。我假设正在发生一些缓冲(因为大尺寸会快速填充缓冲区,而小尺寸则不会),并且我不知道如何禁用此缓冲。有人知道怎么做吗?
代码:
ignore_user_abort(false);
$boundary = "myboundary";
//Set this so PHP doesn't timeout during a long stream
set_time_limit(0);
$socketConn = @fsockopen ("192.168.1.6", 1989, $errno, $errstr, 2);
if (!$socketConn)
exit();
stream_set_timeout($socketConn, 10);
fputs ($socketConn, "GET /mjpeg HTTP/1.0\r\n\r\n");
//Setup Header Information
header("Cache-Control: no-cache");
header("Cache-Control: private");
header("Pragma: no-cache");
header("Content-type: multipart/x-mixed-replace; boundary=$boundary");
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);
stream_set_blocking($f2, false);
//Send data to client
while (connection_status() == CONNECTION_NORMAL)
{
$chunk = fread($socketConn, 128);
print $chunk;
}
fclose($socketConn);
I wrote a simple relay script that connects to a web camera and reads from the socket, and outputs this data using the print function. The data is MJPG data with boundaries already setup. I just output the data that is read.
The problem is PHP seems to be buffering this data. When I set the camera to 1 FPS, the feed will freeze for 7-8 seconds, then quickly display 8 frames. If I set the resolution to a huge size, the camera move at more or less 1 frame per second. I assume then some buffering is happening (since huge sizes fill the buffer quickly, and low sizes don't), and I can't figure out how to disable this buffering. Does anyone know how to?
Code:
ignore_user_abort(false);
$boundary = "myboundary";
//Set this so PHP doesn't timeout during a long stream
set_time_limit(0);
$socketConn = @fsockopen ("192.168.1.6", 1989, $errno, $errstr, 2);
if (!$socketConn)
exit();
stream_set_timeout($socketConn, 10);
fputs ($socketConn, "GET /mjpeg HTTP/1.0\r\n\r\n");
//Setup Header Information
header("Cache-Control: no-cache");
header("Cache-Control: private");
header("Pragma: no-cache");
header("Content-type: multipart/x-mixed-replace; boundary=$boundary");
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);
stream_set_blocking($f2, false);
//Send data to client
while (connection_status() == CONNECTION_NORMAL)
{
$chunk = fread($socketConn, 128);
print $chunk;
}
fclose($socketConn);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
tl;dr version
做两件事:
禁用用户空间输出缓冲区,或者...
在全球范围内,无论是...
关闭 php.ini 中的
output_buffering
,或者使用以下命令关闭 Apache 配置中的
output_buffering
或者只是您关心的脚本,通过...
ob_end_flush()
,或ob_end_clean()
ob_implicit_flush()
,或者echo
语句或其他将输出添加到响应正文的语句之后调用flush()
较长版本
令人困惑的是,有两层可能相关的缓冲,并且 PHP 文档在区分这两层方面做得很差。
输出缓冲区
第一层通常被 PHP 文档称为“输出缓冲区”。这一层缓冲仅影响 HTTP 响应正文的输出,而不影响标头。您可以使用
ob_start()
,然后使用
ob_end_flush()
或ob_end_clean()
。您还可以使用output_buffering
选项。php.ini 的生产版本<的此选项的默认值/a> 是 4096,这意味着输出的前 4096 字节将被缓冲在输出缓冲区中,此时它将被刷新并关闭输出缓冲。
使用,如果您使用的是 Apache)来全局禁用这一层缓冲
您可以通过在 php.ini 文件中将
output_buffering
设置为Off
(或者在 Apache 配置中 。或者,您可以通过在脚本开头调用ob_end_clean()
或ob_end_flush()
来禁用单个脚本。写入缓冲区和 Web 服务器缓冲区
除了输出缓冲区之外,PHP 手册中还提到了“写入缓冲区”,以及 Web 服务器具有的任何缓冲系统。如果您通过
mod_php
将 PHP 与 Apache 结合使用,并且不使用mod_gzip
,则可以调用flush()
刷新这些;对于其他后端,它也可能有效,尽管手册对于给出保证很谨慎:还有几种方法可以让 PHP 在每次
echo
任何内容(或执行任何其他将输出回显到响应正文的操作)时自动调用flush()
。第一种是调用
ob_implicit_flush()
。请注意,该函数的名称具有欺骗性;鉴于其
ob_
前缀,任何有理智的人都会期望它会影响“输出缓冲区”,ob_start
、ob_flush
等也是如此。但是,事实并非如此;ob_implicit_flush()
与flush()
一样,影响服务器级输出缓冲区,并且不会以任何方式与其他ob_< 控制的输出缓冲区交互/代码> 功能。
第二种是通过设置 < 全局启用隐式刷新在 php.ini 中将 code>implicit_flush 标志设置为
On
。这相当于在每个脚本开始时调用ob_implicit_flush()。请注意,该手册建议不要这样做,隐晦地引用了“严重的性能影响”,其中一些我在切线相关的答案。tl;dr version
Do two things:
Disable the userspace output buffer, either...
Globally, by either...
Turning off
output_buffering
in your php.ini, orTurning off
output_buffering
in your Apache config usingor for just the script you care about, by either...
ob_end_flush()
, orob_end_clean()
Also, disable the server-level output buffer as much as you possibly can, by either:
ob_implicit_flush()
at the start of your script, orflush()
after everyecho
statement or other statement that adds output to the response bodyLonger version
Confusingly, there are two layers of buffering that may be relevant and the PHP documentation does a poor job of distinguishing between the two.
The output buffer
The first layer is usually referred to by the PHP docs as the 'output buffer'. This layer of buffering only affects output to the body of the HTTP response, not the headers. You can turn on output buffering with
ob_start()
, and turn it off withob_end_flush()
orob_end_clean()
. You can also have all your scripts automatically start with output buffering on using theoutput_buffering
option in php.ini.The default value of this option for production versions of php.ini is 4096, which means that the first 4096 bytes of output will be buffered in the output buffer, at which point it will be flushed and output buffering is turned off.
You can disable this layer of buffering globally by setting
output_buffering
toOff
in your php.ini file (or usingin your Apache config, if you're using Apache). Alternatively, you can disable it for a single script by calling
ob_end_clean()
orob_end_flush()
at the start of the script.The write buffer, and the webserver buffer
Beyond the output buffer is what the PHP manual refers to as the 'write buffer', plus any buffering system your web server has. If you're using PHP with Apache through
mod_php
, and are not usingmod_gzip
, you can callflush()
to flush these; with other backends, it might work too, although the manual is cagey about giving assurances:There are also a couple of ways you can make PHP automatically call
flush()
every time youecho
anything (or do anything else that echoes output to the response body).The first is to call
ob_implicit_flush()
. Note that this function is deceptively named; given itsob_
prefix, any reasonable person would expect that it would affect the 'output buffer', as doob_start
,ob_flush
etc. However, this is not the case;ob_implicit_flush()
, likeflush()
, affects the server-level output buffer and does not interact in any way with the output buffer controlled by the otherob_
functions.The second is to globally enable implicit flushing by setting the
implicit_flush
flag toOn
in your php.ini. This is equivalent to callingob_implicit_flush()
at the start of every script. Note that the manual advises against this, cryptically citing "serious performance implications", some of which I explore in this tangentially related answer.您可以在每次之后调用
flush()
,而不是禁用输出缓冲读操作。这可以避免弄乱服务器配置并使您的脚本更加可移植。Rather than disabling output buffering, you can just call
flush()
after every read operation. This avoids having to mess with the server configuration and makes your script more portable.输出缓冲可以分层,我曾经遇到过早期代码做了多个级别的情况。这将清除它们。
Output buffering can be layered and I've had cases where earlier code had made multiple levels. This will clear them all.
我们可以在 .htaccess 文件中提供以下代码以禁用 PHP 中的输出缓冲
We can give the below code in the .htaccess file for disabling output buffering in PHP
对于 Windows IIS 服务器,上述解决方案都不起作用,因为 IIS 管理自己的缓冲区,也应该禁用它。
只需将以下
web.config
添加到带有 PHP 脚本的文件夹即可禁用缓冲。在下面的示例中使用 PHP v7.3.7,但您可以将名称替换为其他版本。For Windows IIS-servers none of solutions above work, because IIS manages its own buffers, which should be disabled too.
Just add the following
web.config
to your folder with PHP scripts to disable buffering. In example below used PHP v7.3.7, but you may replace the name to another version.我知道这个问题有点老了,但是回到这个问题,您可以在脚本到脚本的基础上关闭输出缓冲,如下所示:
这应该关闭其后面的任何脚本的所有输出缓冲。
I know this question is a bit old, but coming back to this question, you can turn off output buffering on a script to script basis, like this:
This should turn off all output buffering for any script that follows it.