echo and flush() in PHP show nothing until the script finishes
Output from echo appears all at once when the script endsShort answer
flush() only empties PHP's own system buffers. It cannot empty a PHP output buffer, and the manual says it may not override the web server's buffering or the browser's. Find out which layer holds the data with curl -N, then call ob_flush() before flush() or turn off nginx buffering for that response.
Fix it fast
1Find out which cause you have
Request the page with curl and its buffering switched off. If the lines arrive one by one, PHP and the server are fine: the manual says flush() has no effect on client-side buffering in the browser, so the browser is the one holding the output. If they arrive all at once, one of the causes below applies.
curl -N https://example.com/stream.php2Match what you see, then jump to its fix
- 1
curl -N shows nothing until the end, and a debug line shows ob_get_level() above 0A PHP output buffer is active, and flush() does not empty itFix - 2
curl -N still shows nothing until the end, although the PHP check above shows no active buffer; the site runs behind nginxnginx buffers the FastCGI or proxied responseFix
- 1
What does the error look like?
Output from echo appears all at once when the script endsflush() has no visible effect in the browserob_flush() raises an E_NOTICE when no output buffer is active
Cause 1 of 2
A PHP output buffer is active, and flush() does not empty it
1Check
output_buffering can be switched on for every script, and any code can add its own buffer with ob_start(). The manual says flush() has no effect on user-level output handlers started by ob_start(). Print the nesting level at the top of the script.
<?php
// temporary, at the top of stream.php
header('Content-Type: text/plain');
echo 'output_buffering=', var_export(ini_get('output_buffering'), true), ' level=', ob_get_level(), "\n";2Fix
Empty the PHP buffer first, then the system buffers: ob_flush() followed by flush(). Call ob_flush() only while a buffer exists, because without one it raises an E_NOTICE. For a script that streams the whole time, set output_buffering to 0 in php.ini instead.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "step $i\n";
if (ob_get_level() > 0) {
ob_flush();
}
flush();
sleep(1);
}3Verify
Run the same curl command again; one line should appear per second.
curl -N https://example.com/stream.phpConfirmed by php.net
Cause 2 of 2
nginx buffers the FastCGI or proxied response
1Check
With buffering on, which is the default, nginx collects the response from PHP-FPM in memory buffers before it passes it on. Look for buffering settings in the site config.
grep -R -n -E "fastcgi_buffering|proxy_buffering" /etc/nginx/2Fix
Turn buffering off for the location that streams, or let the script switch it off for its own response with the X-Accel-Buffering header, which nginx reads from the upstream response. Use proxy_buffering instead of fastcgi_buffering if nginx proxies to Apache.
# nginx, in the location that serves the script
fastcgi_buffering off;
// or in the PHP script, before the first echo
header('X-Accel-Buffering: no');3Verify
Reload nginx and run curl -N again.
sudo nginx -t && sudo systemctl reload nginx
curl -N https://example.com/stream.phpConfirmed by nginx.org
How do I stop it coming back?
- Test streaming with curl -N first, so you know which layer holds the data before you touch any setting; a browser page is a poor test.
- Check ob_get_level() before calling ob_flush(), so a missing buffer does not raise a notice.
- Keep streaming endpoints on their own nginx location, so buffering can be off there and on for the rest of the site.
- Do not turn implicit_flush on for a whole site; the manual says it has serious performance implications on the web and is meant for debugging.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Back end
PHP Notice: ob_end_flush(): failed to send buffer of zlib output compression
Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in
The notice comes from code that calls ob_end_flush() while zlib output compression is also handling the output.
3 causes, each with a fixBack end
Chrome shows net::ERR_INCOMPLETE_CHUNKED_ENCODING on a PHP page
The page loads partly, then the response stops
The browser got a chunked response that ended before its final zero-length chunk, so the connection closed mid-page.
2 causes, each with a fixBack end
CodeIgniter works locally but every page is 404 on the live server
The requested URL was not found on this server.
A 404 on the live server comes from one of four places: the document root is not the public folder, Apache ignores .htaccess or nginx has no try_files rule, the route is not defined because auto routing is off, or the controller file name differs from its class in case.
5 causes, each with a fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack end
Laravel routes return 404 or throw NotFoundHttpException
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
A 404 means Laravel found no route for that URL, or the request never reached Laravel at all.
3 causes, each with a fixBack end
Livewire uploads work locally but fail behind nginx on the server
$_POST and $_FILES are empty (PHP discards a body over post_max_size)
Three separate limits stop a large upload, and a local dev server hides two of them.
3 causes, each with a fix