Skip to content

Instantly share code, notes, and snippets.

@hranicka
Last active May 5, 2023 14:52
Show Gist options
  • Save hranicka/368dfbf3df971f021fa70047eb894058 to your computer and use it in GitHub Desktop.
Save hranicka/368dfbf3df971f021fa70047eb894058 to your computer and use it in GitHub Desktop.
PHP flush() under Apache, mod_proxy, php-fpm
<?php
function doFlush()
{
if (!headers_sent()) {
// Disable gzip in PHP.
ini_set('zlib.output_compression', 0);
// Force disable compression in a header.
// Required for flush in some cases (Apache + mod_proxy, nginx, php-fpm).
header('Content-Encoding: none');
}
// Fill-up 4 kB buffer (should be enough in most cases).
echo str_pad('', 4 * 1024);
// Flush all buffers.
do {
$flushed = @ob_end_flush();
} while ($flushed);
@ob_flush();
flush();
}
// In a real app, we have this turned on.
ob_start();
$i = 0;
while ($i++ < 10) {
usleep(500000);
// Real output.
echo $i . "\n<br>";
doFlush();
}
@cookieguru
Copy link

Ahh, I see, I forgot about the other params during my late night coding session 🙂
Effectively it's being used like str_repeat in this case

@laborc8
Copy link

laborc8 commented May 15, 2022

great solution - works! - I love it - Thanks a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment