Last active
May 5, 2023 14:52
-
-
Save hranicka/368dfbf3df971f021fa70047eb894058 to your computer and use it in GitHub Desktop.
PHP flush() under Apache, mod_proxy, php-fpm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} |
I agree this may not be the best possible solution, I haven't used this since 2016 anyway :-)
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
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
Hi, please see documentation of str_pad.
Basically it takes empty string
''
and adds 4096 times space character (third, optional argument ofstr_pad
).Here is an example of string padded to 4096 bytes: https://3v4l.org/MAIV8