Methods ob_start and ob_flush don't work, why?

前端 未结 7 1061
时光取名叫无心
时光取名叫无心 2020-12-29 11:49

I am using ob_start()/ob_flush() to, hopefully, give me some progress during a long import operation.

Here is a simple outline of what I\'m

相关标签:
7条回答
  • 2020-12-29 12:17

    Make sure that your output buffering doesn't start automatically. Run:

    print ob_get_level ();
    

    before ob_start (); if will will see something else then 0 you've got the answer.

    0 讨论(0)
  • 2020-12-29 12:21

    You also need to check the PHP settings

    some installs default to 4096, some default to off

    output_buffering = Off
    output_buffering = 4096

    agreed with George but do check the above settings

    0 讨论(0)
  • 2020-12-29 12:21

    Ob_end_clean() discards the contents of the current output buffer and turns off the buffering. You should use ob_end_flush() instead.

    0 讨论(0)
  • 2020-12-29 12:29

    It's possible that your webserver is doing its own buffering. Probably with something like mod_gzip.

    Here is some very simple test code:

    <?php
    echo 'starting...<br/>';
    for($i = 0; $i < 5; $i++) {
      print "$i<br/>";
      flush();
      sleep(2);
    }
    print 'DONE!<br/>';
    

    If it takes 10 seconds for that page to load, rather than seeing a new line every 2 seconds, then it means that it is being cached by your webserver. For what you are trying to do, there is no need to use ob_start and ob_flush. Just call flush whenever you want to force the content to the browser. However, like I mentioned, if the webserver is waiting for the content to complete before sending, then that won't do anything for you.

    Edit: Another possibility is that you're viewing the page from behind a corporate or ISP proxy/firewall that waits for the whole page before serving it (so that it can scan it to see if it looks like pornography, for example).

    0 讨论(0)
  • 2020-12-29 12:32

    Hey man I was also got stuck in this problem and finally got the correct solution here it is for you

    you have to add content type for your page you can do that by two ways 1. using html tag

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    

    Ex.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Wp Migration</title>
    </head>
    <body>
    <?php 
    for($i=0;$i<70;$i++)
    {
    echo 'printing...<br>';
    ob_flush();
    flush();
    sleep(3);
    }
    ?>
    </body>
    </html>
    
    1. using php header function

      <?php header( 'Content-type: text/html; charset=utf-8' ); ?>

    Ex.

    <?php 
    header( 'Content-type: text/html; charset=utf-8' );
    for($i=0;$i<70;$i++)
    {
    echo 'printing...<br>';
    ob_flush();
    flush();
    sleep(3);
    }
    ?>
    

    All the best

    0 讨论(0)
  • 2020-12-29 12:35

    You can edit it with the .htaccess file

    To disable output buffering, modify the line as follows:

    php_value output_buffering Off
    php_value output_buffering 4096
    

    worked for me. Thank you!

    Check this site: Click Here

    0 讨论(0)
提交回复
热议问题