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.

2
  • 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. Commented Apr 22, 2011 at 11:52
  • This worked for me in codeigniter - stackoverflow.com/a/4978809/2083877 Commented Mar 7, 2019 at 10:02

3 Answers 3

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.