3

I have this code:

<?php
for($i = 0; $i<30; $i++)
{
    echo "$i<br>";          
    usleep(100000); 
}

?>

I would like to know how make the browser display 0, then 1, then 2, ...

In my example, it loads for a while and then display 0-29.

Gabriel Bianconi
  • 1,179
  • 3
  • 19
  • 37

6 Answers6

5

Disable all output buffering and pad the output:

while(@ob_get_clean());

for($i = 0; $i<30; $i++)
{
    echo str_pad("$i<br>",4096);          
    usleep(100000); 
}

Also, this won't work, if your Apache is using mod_deflate and you have gzip-compression for text/html files.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • what is the purpose of str_pad? – Caner Dec 09 '10 at 15:03
  • 2
    Output isn't sent immediately by Apache. It is sent in chunks of a certain size; hence the padding. – Linus Kleen Dec 09 '10 at 15:05
  • Thanks, this worked. Is there a way to remove mod_deflate and gzip from one single file and make them work the other files? – Gabriel Bianconi Dec 09 '10 at 15:06
  • Yes. You'll have to activate it locally (not in the `httpd.conf`) by using an `.htaccess` file in a directory in which you'd like compression and put the scripts that aren't to be compressed in a different directory. – Linus Kleen Dec 09 '10 at 15:10
  • Is there a way to disable gzip & deflate from one directory instead? How can I do this? Can this be done with PHP? If not, .htaccess? Thanks. – Gabriel Bianconi Dec 09 '10 at 15:14
  • 1
    It can be achieved w/ an `.htaccess` file. In a `` block, put `RemoveOutputFilter .php`. The output will not be handled by `mod_deflate` then. – Linus Kleen Dec 09 '10 at 15:20
  • Thanks. To remove gzip, I did this: SetEnvIfNoCase Request_URI FILE_NAME\.php$ no-gzip dont-vary It's working. – Gabriel Bianconi Dec 09 '10 at 15:21
  • is there any way to replace `str_pad` in order not to affect the page layout (if I want to display as plain text) ? – Raptor Mar 25 '13 at 04:11
  • @ShivanRaptor You mean `Content-Type: text/plain` or "plain" text in `
    `? The former, at least, cannot be achieved to my knowledge.
    – Linus Kleen Mar 25 '13 at 07:47
  • i'm talking about former one. Oops, how about appending invisible ASCII codes ? – Raptor Mar 25 '13 at 11:09
  • Try the ["invisible separator"](http://www.fileformat.info/info/unicode/char/2063/index.htm) (`\u2063`) and let me know how it worked out. – Linus Kleen Mar 25 '13 at 12:42
1

Another perhaps quicker way is to do

<?php ob_implicit_flush(true); ?> 

to tell php to flush the output buffer after each output, not at the end of the file being processed.

jrble819
  • 119
  • 1
  • 9
1

Flush the output buffer manually:

<?php
ob_start();
for($i = 0; $i<30; $i++)
{
    echo "$i<br>";
    ob_flush();
    usleep(100000); 
}
?>
hupf
  • 604
  • 1
  • 6
  • 10
0

In my experience there is no reliable way to make a webpage send output data in realtime.

There are many different places where it can go wrong - and while you can easily get it to work on a single situation there will always be other places where the solution does not work.

Ultimately the only reliable solution is to execute your php code from a shell prompt (via SSH or whatever) and completely bypass apache and web browsers.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
0

Use javascript. HTML isn't an interactive console.

EDIT: Those of you downvoting, please remember that I am proposing an actual working solution. Incremental output from php is in no way a valid solution here, because there are some many places along the way where it could get buffered, and browsers are in no way obligated to render html incrementally as it comes in.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
  • He wasn't asking about HTML. This question is about PHP. – Linus Kleen Dec 09 '10 at 15:00
  • he clearly IS talking about html. See the
    tag in the output?
    – Tyler Eaves Dec 09 '10 at 15:01
  • ...and yet, it's still PHP. Think about non-blocking FTP uploads processed by a PHP file. It can be quite interactive then. – Linus Kleen Dec 09 '10 at 15:14
  • He wants to use PHP but he can't. This is a javascript effect. He needs to use setTimeout(). PHP will render his code and then send all the HTML as once to the client. That's what PHP do in combation with Apache. You can use PHP-CLI, that will work but only in console mode. – Stijn Leenknegt Dec 09 '10 at 15:19
0
<?php
header('Content-type: text/html; charset=utf-8');
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);

echo "Yes";
sleep(5);
echo " sir";

Don't ask me to explain it, but this works for me.

William Entriken
  • 37,208
  • 23
  • 149
  • 195