php flush()有时不起作用,重新加载它运行的页面

时间:2015-05-04 12:30:02

标签: php flush

我无法理解代码中的问题,我认为这与flush()相关 它有时可以工作,有时不工作,但如果我重新加载页面,
(然后点击"继续"在确认表单提交弹出窗口中),它有效!

这是我的代码:

Class.php:

private function myFunUpdate($aaa, $bbb, $ccc, $ddd, $eee){
    $httpCode = curl_getinfo($aaa, CURLINFO_HTTP_CODE);
    if ($httpCode == "200"){
        $percent = @round($ccc/$bbb, 2) * 100;
        if ($percent > $this->_percentDownloaded){
            $this->_percentDownloaded++;
            echo '<script>myFunUpdate("'. $percent .'");</script>';
            ob_end_flush();
            ob_flush();
            flush();
        }
    }
}

的index.php:

<?php
    if ($_POST['submit']){
        echo '<div><img src="http://example.com/img'.$alpha->stuff1(trim($_POST['myURL'])).'.jpg" /></div>';
        echo '<div>'.$alpha->stuff2(trim($_POST['myURL']), 'url').'</div>';
        echo '<div id="progressBar">0%</div>';
        flush();

        if ($alpha->stuff3(trim($_POST['myURL']))){
            echo '<div id="divSuccess"></div>';
            echo '<script>var progressBar = document.getElementById("progressBar"); progressBar.style.width = progressBar.innerHTML = "0%"; updateProgress("'.trim(strstr($alpha->myFun(), '/'), '/').'");</script>';
            flush();
            $alpha->stuff4($_POST['param1']);
        }else{
            echo '<p>Error, something was wrong...</p>';
        }
    }
?>

在php日志文件中我找到了这个(指的是 Class.php 行):

PHP Notice:  ob_end_flush(): failed to delete and flush buffer. No buffer to delete or flush in xxx.php on line xxx

PHP Notice:  ob_flush(): failed to flush buffer. No buffer to flush in xxx.php on line xxx

1 个答案:

答案 0 :(得分:0)

这可能是php.net

的原因
  

如果你调用ob_flush()和flush()仍然没有得到缓冲区   脸红了可能是因为有些杀毒软件(Panda在此   case)保持缓冲区,直到页面完成之前加载   将其发送到浏览器。

在php代码的开头包含标题似乎也很重要:

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

好的,我会尝试使用您的代码:

private function myFunUpdate($aaa, $bbb, $ccc, $ddd, $eee){
    $httpCode = curl_getinfo($aaa, CURLINFO_HTTP_CODE);
    if ($httpCode == "200"){
        $percent = @round($ccc/$bbb, 2) * 100;
        if ($percent > $this->_percentDownloaded){
            $this->_percentDownloaded++;

            // start output buffering
            if (ob_get_length() === false) {
             ob_start();
            }
            echo '<script>myFunUpdate("'. $percent .'");</script>';

            while (ob_get_level()) {
             ob_end_flush();
            }
        }
    } }

<?php
    header( 'Content-type: text/html; charset=utf-8' );
    // start output buffering
    if (ob_get_length() === false) {
      ob_start();
    }
    if ($_POST['submit']){
        echo '<div><img src="http://example.com/img'.$alpha->stuff1(trim($_POST['myURL'])).'.jpg" /></div>';
        echo '<div>'.$alpha->stuff2(trim($_POST['myURL']), 'url').'</div>';
        echo '<div id="progressBar">0%</div>';

        while (ob_get_level()) {
            ob_end_flush();
        }

        if ($alpha->stuff3(trim($_POST['myURL']))){
            // start output buffering
            if (ob_get_length() === false) {
              ob_start();
            }
            echo '<div id="divSuccess"></div>';
            echo '<script>var progressBar = document.getElementById("progressBar"); progressBar.style.width = progressBar.innerHTML = "0%"; updateProgress("'.trim(strstr($alpha->myFun(), '/'), '/').'");</script>';
            while (ob_get_level()) {
               ob_end_flush();
            }
            $alpha->stuff4($_POST['param1']);
        }else{
            echo '<p>Error, something was wrong...</p>';
        }
    }
?>