2

Possible Duplicate:
what is output buffering?
Why does PHP send it all at once?

I want to echo "sent" dynamically when sending emails from list. My script only echos after it finishes all. How can I echo string dynamically in while loop ?

$f=fopen("list.txt","r");

if($f){

    while (!feof($f)) {

        $line = fgets($f, 4096);

        $lncount++;

        $val = $lines[$lncount];

        mailsender($line,$yollayan,$sifresi,$name);

        echo "sent";

    }
}
Community
  • 1
  • 1
user198989
  • 4,574
  • 19
  • 66
  • 95

5 Answers5

3

You will need to set up the output for buffering. Try the script below. Make sure you switch of zipping of your output to see real time output.

@apache_setenv('no-gzip', 1); 
@ini_set('zlib.output_compression', 0);
ob_implicit_flush(true);
ob_end_flush();

$f=fopen("list.txt","r");

if($f){

   while (!feof($f)) {

        $line = fgets($f, 4096);
        $lncount++;
        $val = $lines[$lncount];
        mailsender($line,$yollayan,$sifresi,$name);
        echo " sent";
    }
}
Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
1

Try this

  echo "Sent ";
  echo str_repeat("\n",1024);
  flush();

From the PHP Manual:

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. [...]

Server modules for Apache like mod_gzip may do buffering of their own that will cause flush() to not result in data being sent immediately to the client.

Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line or the beginning of a tag, and it won't render tables until the tag of the outermost table is seen.

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.

Makesh
  • 1,236
  • 1
  • 11
  • 25
1

My script only echos after it finishes all.

It probably does - but you've not provided nearly enough information to determine if this is the case.

Most likely your question is a duplicate of this one.

If you add a flush() after the echo and run it from the command line you'll see the progress.

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94
1

You can use flush(); below the echo to send this to the browser: http://php.net/manual/en/function.flush.php (read the comments on the link to get more info).

Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
0

The effect you're seeing is output buffering. Call the flush() function to force php to send pending output.

Do read the different caveats and possible limitations that can arise in different scenarios and with different server types.

fvu
  • 32,488
  • 6
  • 61
  • 79
  • I remember I had problems using it on my xampp server. – Leri Jul 20 '12 at 12:42
  • @PLB see the php manual page I refer to, it's unfortunately not as simple as just slapping flush() at the end of the loop, there are several platform dependencies and interactions you may have to cope with. – fvu Jul 20 '12 at 12:45