1

The issue I am having I am am needing to steam output when I echo something in a loop. Now typically I would put something such as

ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);

ob_end_flush();
while (@ob_end_flush());

ini_set('implicit_flush', true);
ob_implicit_flush(true);

header("Content-type: text/html");
header('Cache-Control: no-cache');

at the top of the page and then call

echo 'Something to print out here';
ob_flush();
flush();

However this is not working at all. I get no errors generated or shown it just doesn't output immediately as desired without buffering. It seems to have no effect at all. I have also tried altering the php.ini file. This also has no effect. I have tried this on 2 different versions of PHP Desktop. I have tried it on PHP Desktop 47.0 Chrome which uses PHP 5.4 and the lastest that just came out PHP Desktop 57.0 which utilizes PHP 7. Any insight would be greatly appreciated.

UPDATE I received a response back from the developer of php desktop and he didn't know why it wasnt working and suggested that Mongoose web server which php desktop utlizes may not support this. Would anyone have more experience with Mongoose than I? I have never really used it other than when using php desktop

Micha
  • 383
  • 3
  • 14
  • When you say ‘immediately’ – do you mean without refreshing the page? – Zoe Edwards Mar 14 '18 at 17:23
  • I should edit that, what i meant is output right away without buffering. I dont mind if the page refreshing. I just need the data to output without the wait – Micha Mar 14 '18 at 17:25
  • I don't get it, what wait? – ChristianM Mar 14 '18 at 17:27
  • Could you just disable the output buffer in php.ini and it will echo during script runtime? – Ice76 Mar 14 '18 at 17:35
  • How do you know it’s not displaying before buffering? – Zoe Edwards Mar 14 '18 at 17:39
  • @Ice76 Thats the thing. I have disabled it. It doesnt effect it even when I turn it off – Micha Mar 14 '18 at 17:39
  • @ThomasEdwards because its waiting 20 seconds before outputing when 20 percent of the function is done. So basically it doesnt print each time the loop executes. it happens lets say every 20th time outputing the info up to that point and so and an so forth – Micha Mar 14 '18 at 17:41
  • 20 seconds?! I don’t think this is the problem, there must be something else going on? – Zoe Edwards Mar 14 '18 at 17:41
  • I also notice it when i use glob() and output the name of each file. It does the same thing. It will go through several files until it outputs a chunck of the names instead of one by one like I wanted. – Micha Mar 14 '18 at 17:46

1 Answers1

2

The trick to make output buffer flush work in Mongoose is to output a total of 8192 characters before calling ob_flush / flush. Example code below, read php comments and html comments for more details.

<?php

error_reporting(-1);

ini_set('zlib.output_compression', 0);
ini_set('output_buffering', 0);
ini_set('implicit_flush', 1);

// This buffer length value is copied from "mongoose.c" file.
// If you would like to reduce buffer size then modify "mongoose.c"
// file and rebuild phpdesktop from sources.
define("MG_BUF_LEN", 8192);

function fprint($s)
{
    $args = func_get_args();
    call_user_func_array('printf', $args);
    print(str_repeat(" ", MG_BUF_LEN));
    @ob_flush();
    flush();
}

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");

?>

<style type="text/css">@import url("style.css");</style>
<a href="index.php">Go back to index</a>
| <a href="<?php echo $_SERVER["REQUEST_URI"];?>">Refresh</a>

<title>Output buffer flush</title>
<h1>Output buffer flush</h1>

<p>
    This example forces flush of output buffer repeatedly.
</p>
<p>
    This technique works differently with Mongoose web server.
    Mongoose forces to always read 8192 characters (MG_BUF_LEN)
    before sending output. The solution is to output 8192 spaces
    before each ob_flush / flush calls. Spaces are ignored in html,
    so this output is not visible. It should really be 8192
    characters minus characters previously outputted for it to
    work always correctly. In this simple examples this is not
    required.
</p>

<?php

fprint("\r\n\r\n");
sleep(1);
fprint("Slept for 1 second<br>");
sleep(2);
fprint("Slept for 2 seconds<br>");
sleep(3);
fprint("Slept for 3 seconds<br>");
fprint("Done.")

?>

I've commited the "ob-flush.php" example to phpdesktop:

https://github.com/cztomczak/phpdesktop/blob/2a800fecd8830e4f1e6c4054e74e8d03b4900847/phpdesktop-chrome57/www/ob-flush.php

Czarek Tomczak
  • 20,079
  • 5
  • 49
  • 56
  • This doesn't solve the issue what so ever. It is just another "hacky" work around to php desktop which there seems to be many. When tested on any script of significant size, the problem remains. If the buffer size in mongoose can be adjusted to resolve this why wasnt this done to begin with instead of suggesting that it be adjust and rebuilt by the user from "sources" that obviously arent available. – Micha Jan 15 '19 at 19:51
  • @Micha If you don't like it, don't use it. I won't help you anymore. – Czarek Tomczak Jan 17 '19 at 09:24
  • You didnt in the first place. You were a prick when i asked simple questions. You really have a great way of trying to get people to support your project hense why i see your funding has gone nowhere – Micha Jan 18 '19 at 09:24