39

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);
Ioncannon
  • 901
  • 1
  • 7
  • 19
  • Show us some relevant code! We can't guess at what you're doing. Or rather, we can, but it probably won't help much. –  Jan 16 '12 at 15:46
  • only this answer has worked for me on the whole internet - https://stackoverflow.com/a/4978809/2083877 – Sunil Kumar Mar 07 '19 at 09:58

6 Answers6

69

tl;dr version

Do two things:

  1. Disable the userspace output buffer, either...

    • Globally, by either...

      • Turning off output_buffering in your php.ini, or
      • Turning off output_buffering in your Apache config using

        php_flag "output_buffering" Off
        
    • or for just the script you care about, by either...

      • calling ob_end_flush(), or
      • calling ob_end_clean()
  2. Also, disable the server-level output buffer as much as you possibly can, by either:

    • calling ob_implicit_flush() at the start of your script, or
    • calling flush() after every echo statement or other statement that adds output to the response body

Longer 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 with ob_end_flush() or ob_end_clean(). You can also have all your scripts automatically start with output buffering on using the output_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 to Off in your php.ini file (or using

php_flag "output_buffering" Off

in your Apache config, if you're using Apache). Alternatively, you can disable it for a single script by calling ob_end_clean() or ob_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 using mod_gzip, you can call flush() to flush these; with other backends, it might work too, although the manual is cagey about giving assurances:

Description

void flush ( void )

Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats.

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

There are also a couple of ways you can make PHP automatically call flush() every time you echo 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 its ob_ prefix, any reasonable person would expect that it would affect the 'output buffer', as do ob_start, ob_flush etc. However, this is not the case; ob_implicit_flush(), like flush(), affects the server-level output buffer and does not interact in any way with the output buffer controlled by the other ob_ functions.

The second is to globally enable implicit flushing by setting the implicit_flush flag to On in your php.ini. This is equivalent to calling ob_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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
  • 1
    When running Apache with PHP-FPM, FastCGI adds an additional buffer with default size 65537 (FcgidOutputBufferSize) . – Simon Jul 16 '20 at 08:04
20

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.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • 7
    In addition, `ob_flush()` is sometimes also needed. – Brad Jan 16 '12 at 15:47
  • 1
    Indeed, although I *think* this should only be necessary if you have called `ob_start()` - I seem to remember it emits a warning otherwise. – DaveRandom Jan 16 '12 at 15:48
  • I tried flush before, but it did not seem to affect the buffering. I put some code in the original post – Ioncannon Jan 16 '12 at 17:10
  • 2
    There is probably no point in making your socket non-blocking, this may cause a race condition with your code. The `flush()` call should be placed immediately after the `print` call in the loop. If this is still not working, please show the output of `ini_get('output_buffering');` and `ini_get('zlib.output_compression');` – DaveRandom Jan 16 '12 at 17:18
  • `ini_get('output_buffering');` returns 1 `ini_get('zlib.output_compression');` returns 0. I modified the code to run `ini_set('zlib.output_compression', 0); ini_set('implicit_flush', 1);` but strangely `ini_set('output_buffering', 0);` does not set itself to 0. – Ioncannon Jan 16 '12 at 17:43
  • 2
    That is because by the time you execute `ini_set('output_buffering', 0);` the script is already running and the output buffer has already started - it is not changable at run time. Can you set it in a .htaccess file? This should be a work around, if you host allows it. You will probably still need to `flush()`. – DaveRandom Jan 16 '12 at 17:48
  • I see, makes sense. This is what should be in the .htaccess right? `php_value "output_buffering" "0"` – Ioncannon Jan 16 '12 at 17:57
  • 1
    Yep, should do the trick as long as 1) you are running Apache 2) you are running PHP as a module, not in CGI mode. If either of these are not true, you will need to modify php.ini, which should be avoided if possible as this is a site- (or possibly server-) wide change. Not sure if the quotes are necessary or valid, just have a play around with it. – DaveRandom Jan 16 '12 at 17:59
  • Going to have to ask the guy who setup the server how PHP is being run. I threw the .htaccess into the same folder as the script, and put in the above line. I also added flush() and ob_flush() after the print line. Still have buffering issues. I read that doing this requires an apache reset? Going to get the guy to modify the php.ini file if the reset doesn't work. – Ioncannon Jan 16 '12 at 18:06
  • 1
    .htaccess should not require Apache restart - this issue may be simply that you are not outputting enough data on each iteration and PHP/Apache has a minimum payload requirement for TCP packets that you are not meeting - although that is pure conjecture. – DaveRandom Jan 16 '12 at 18:18
  • Well, Output_Buffering is 0, and it still is happening. PHP is running in FastCGI mode. Any other ideas why the output is being buffered? I can't think of anything else. Isn't the TCP payload like 512bytes? Each jpg frame is about 8kb! Anyway, thanks for trying to help, I may port this over to C++ or Java if I can't figure out how to get rid of the buffering problem. – Ioncannon Jan 17 '12 at 20:50
  • Many browsers have an internal buffer of 1024 bytes, and will not display any output until at least that much has been downloaded. Printing out 1024 bytes of whitespace is necessary if you want browsers to display a page as it is executed. – Abhi Beckert Jun 28 '12 at 11:58
18

Output buffering can be layered and I've had cases where earlier code had made multiple levels. This will clear them all.

while (ob_get_level()) ob_end_clean(); 
// or ob_end_flush() if you want the contents of the buffer.
Yehosef
  • 17,987
  • 7
  • 35
  • 56
3

We can give the below code in the .htaccess file for disabling output buffering in PHP

php_flag "output_buffering" off
Binod Kalathil
  • 1,939
  • 1
  • 30
  • 43
1

For Windows IIS-servers none of solutions above doesn't works, because IIS manages its own buffers, which should be disabled too.

Just add 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.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <clear />
            <add name="php-7.3.7" path="*.php" verb="GET,HEAD,POST" modules="FastCgiModule" scriptProcessor="C:\Program Files\PHP\v7.3\php-cgi.exe" resourceType="Either" requireAccess="Script" responseBufferLimit="0" />
        </handlers>
    </system.webServer>
</configuration>
0

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:

if (ob_get_level())
   ob_end_clean();

This should turn off all output buffering for any script that follows it.

Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
  • ...and perhaps generate a notice as it does for me (level=1): PHP Notice: ob_end_clean(): failed to delete buffer. No buffer to delete – Josh Nov 29 '14 at 21:49
  • This worked for me, however, it appears that PHP is starting at level 1 output buffering sometimes. Wasn't the case for me. – Solomon Closson Dec 02 '14 at 20:15
  • 2
    My previous comment is wrong; it turns out there was a later ob_end call in another script I didn't know about (that didn't check the level) that was causing the error. This solution works great if your code is written correctly. :P – Josh Dec 04 '14 at 20:32