Hi,

I have a need for a loader image for my php page. It processes lots of XML code and it takes a while to actually load the page, so I need to put an animated gif that says "Loading ..." while the XML is processed. I'm trying to accomplish this with PHP's ob_flush() and flush() functions. Internet Exploder works like a charm, Firefox on the other hand buffers the whole output and displays it as soon as everything is processed. How do I make it flush the buffers?

Windows XP Professional
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1

Thanks alot.

    it should work. are you using it like this?

    <img src="loading.gif">
    <?
    ob_flush();
    flush();
    
    // process long stuff here
    ?>
    <script>window.location='landing_page.php';</script>;

      you don't need to do ob_flush() unless you are actually buffering output. Just flush() will work, but depending on the browser, you need to send so much information. For example, I believe IE will show at only about 4k of data, but Safari needs about 10-12k. I'm not sure how much firefox needs.

        you would think, wouldn't ya 🙂 .. i have a php 5.0.4 installed on a fedora box and a script that does this. it would not flush until i added the ob_flush(). i did not setup an ob at all. i've seen this issue reported on php.net as well.

        if you're having trouble with flush().. ob_flush() will most likely fix it.

        i think you're right in ephenstian's case though. he's probably not printing enough to the browser.

          mmilano wrote:

          you would think, wouldn't ya 🙂 .. i have a php 5.0.4 installed on a fedora box and a script that does this. it would not flush until i added the ob_flush().

          It must be a 5.0 bug, because just flush() works fine on 5.1.1 and 5.1.2.

          If you need to send some extra data to get a good flush, you can use str_pad:

          echo str_pad('',9096);

          To demonstrate use, here is a quick sample script that outputs numbers 1-6 over a course of ~1 minute (~1 number every 10 seconds). I'm only using flush() and this works fine in PHP 5.1.2. You might have to add ob_flush() for earlier PHP versions as mmilano suggested. I'm using 9096 because originally I needed that much to get Safari to work, however, testing again at 4096 and both Safari and Firefox will work at that level as well. However, I am included to stay a bit above that since Safari did show problems earlier:

          <?php
          
          for ($i = 1; $i <= 6; $i++) { 
          	echo $i . '<br>';
          	echo str_pad('',9096)."\n";
          
          // flush output
          flush();
          
          // sleep.... sleeeppp...
          sleep(10);
          }

            Thanks for the suggestions, except that's exactly what I had and it wasn't working. The problem as it turned out was with mod_gzip in apache. After turning that off, it was working great in Firefox, BUT it wouldn't work in IE 6.0.2800. Worked fine in 6.0.2900, but not 2800. Had to put an exception in mog_gzip for the 2800; kinda silly how Microsoft makes their browsers, but it works now.

            Thanks again.

              8 months later

              With PHP 4.3.x

              I could use ob_flush(), flush() or whatever you want :
              I'm calling a web service (SOAP, using NUSOAP library ) but my preloader image
              wan't be displayed until the page is completely loaded, pratically when it is unusefull.

              Nobody knows another way to build a preloader with php?(or maybe another technology...)
              Thank you

                print the page displaying the preloader. use an AJAX request to get the web service data and then javascript to populate the appropriate areas.

                  Write a Reply...