0

I've read many articles and did research online, but I cannot figure out why those commands are not working on my machine. Code get executed but it echoes when whole script is executed. I am testing it with simple code, I will easily implement it later for everything else. Here is the code:

ob_start();

echo "Start ...<br />\n";
for( $i = 0 ; $i < 5 ; $i++ )
{
    echo $i."<br />\n";
    ob_flush();
    flush();
    sleep(1);
}
echo "End ...<br />\n";

I tried using ob_end_flush(), nothing worked for me. I've checked php.ini for configuration it says:

output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off

I don't know what is wrong, I've also read on php.net "As of August 2012, all browsers seem to show an all-or-nothing approach to buffering. In other words, while php is operating, no content can be shown."

Is my code wrong? Server settings? Browsers? Is there a workaround?

zuboje
  • 696
  • 3
  • 11
  • 28
  • 1
    The quote you posted in your question seems like a definitive answer to your question. **...while php is operating, no content can be shown.** – user229044 Oct 16 '13 at 20:23
  • That's why I asked in the end "Is there a workaround?" – zuboje Oct 16 '13 at 20:24
  • 1
    Also read the [flush() documentation](http://php.net/flush): "[flush()] has no effect on any client-side buffering in the browser" – ComFreek Oct 16 '13 at 20:26
  • possible duplicate of [Calling ob\_flush() and flush(), yet browser doesn't show any output until script finishes](http://stackoverflow.com/questions/5770917/calling-ob-flush-and-flush-yet-browser-doesnt-show-any-output-until-script) – Brandon Oct 16 '13 at 20:27
  • @ComFreek if you read same documentation it is written "This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those. " which is showed in my code above – zuboje Oct 16 '13 at 20:30
  • @BrandonWamboldt that's why I said "zlib.output_compression = Off" READ FIRST. – zuboje Oct 16 '13 at 20:32
  • @zuboje That flushes the output on the PHP side. The browser may still buffer some data (that's what webarto's answer is about: triggering the maximum length of such a buffer). – ComFreek Oct 16 '13 at 20:32

2 Answers2

0

While I don't know the real answer, you can do it like this:

<?php

ob_start();
ob_implicit_flush(false);

echo str_repeat(' ', 1024);

# rest of code here
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
0

Did you try?

ob_implicit_flush(true);
Sajuna Fernando
  • 1,364
  • 9
  • 16
  • I ran your exact code just now. Seems to output just fine. My guess is your check your php.ini to see if object buffering is turned on and make sure the buffer limit is high – Sajuna Fernando Oct 16 '13 at 20:40
  • what are your server settings? What browser are you using? Php version? In addition to this, "Turning this option on has serious performance implications and is generally recommended for debugging purposes only." – zuboje Oct 16 '13 at 20:45
  • Running PHP 5.4 on MAMP on a Mac OSX and on Chrome. I just checked and i do not have implicit_flush turned on either. – Sajuna Fernando Oct 16 '13 at 20:51
  • Try to print the ob_get_level() before ob_start(); You should get 0; – Sajuna Fernando Oct 16 '13 at 20:53
  • I am getting 1. I have PHP 5.3.8, IIS/7.5 – zuboje Oct 16 '13 at 20:56
  • http://php.net/manual/en/function.ob-get-level.php You should have 0 as your object buffering has not yet started. Which mean there is another your current ob_start is nested under another one. Can you paste your whole php script file? – Sajuna Fernando Oct 16 '13 at 20:59
  • that is a whole file because that is just one of the scripts, application is using about 30+ php files... I will try by resetting that to 0 – zuboje Oct 16 '13 at 21:06
  • Check if theres a space before your php tag? – Sajuna Fernando Oct 16 '13 at 21:18