2

I was trying to get codeigniter to output text as the script was working but couldn't get it to work. I have search on here and google and seen using ob_end_flush(); and flush(); and also along with adding more bytes so the browser can output. But none of that is working in CI 2.x. If anyone has had luck with this, thanks in advance

I have tried

function test()
{
    ob_end_flush();
        echo "test1";
    ob_start();
    sleep(3);
    ob_end_flush();
        echo "test1";
    ob_start();
    sleep(3);
    ob_end_flush();
        echo "test1";
    ob_start();
}

With no luck. The script waits 6 seconds then spits everything out at once. I would like it to echo the output to the screen then wait 3 seconds then output the next echo then wait another 3 seconds etc.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
William
  • 1,033
  • 2
  • 13
  • 25
  • Without seeing your code, how can this be solved? With a wild guess, I will say try `ob_get_clean()` but still I have about 1% of an idea what you are having trouble with. Post your actual code and take time to explain your problem clearly, and show what you have tried and specifically what is not working. – Wesley Murch Apr 22 '11 at 11:52
  • This worked for me in codeigniter - https://stackoverflow.com/a/4978809/2083877 – Sunil Kumar Mar 07 '19 at 10:02

3 Answers3

0

The issue you're having with Code Igniter specifically is that there is already an output buffer in effect. Preceding your test with the following snippet will get you out of php-level buffering at least:

// try to bust out of output buffering
while(ob_get_level()) {
    ob_end_flush();
}
ob_end_flush();

As noted by @Wesley, this can still be undermined by your server's configuration, but in my current setup I can stream output back after busting out of all output buffers.

Jerry
  • 3,391
  • 1
  • 19
  • 28
0

check your server api with

echo phpinfo();

if you found your server api

Server API :  CGI/FastCGI

in CentOS Add below line in "/etc/httpd/conf.d/fcgid.conf"

OutputBufferSize 0

Restart your Apache server and try below code

ob_start();
for($i = 0; $i < 10; $i ++) {
        echo $i;
        echo '<br />';
        flush();
        ob_flush();
        sleep(1);
    }
gsm
  • 61
  • 1
  • 6
0

I tried this today and didn't worked either. Then I looked at the core's output class and there was a private _display() function. I figured that the output is collected before it's displayed into some variable then at last this function is called. So before my code in the controller method, I added this line:

$this->output->_display("");

and then ran the code. It worked. So your modified function would be like this :

function test()
{
    $this->output->_display("");
    ob_end_flush();
        echo "test1";
    ob_start();
    sleep(3);
    ob_end_flush();
        echo "test1";
    ob_start();
    sleep(3);
    ob_end_flush();
        echo "test1";
    ob_start();
}
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78