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";

    }
}
1
  • 1
    @symcbean: Let's not have duplicates of duplicates. Commented Jul 20, 2012 at 12:53

5 Answers 5

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

1 Comment

Not worked. Doesn't print anything.
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.

Comments

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).

Comments

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.

1 Comment

Sorry typo mistake ( its flush() not fflush() )
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.

2 Comments

I remember I had problems using it on my xampp server.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.