【问题标题】:php flush() sometimes not work, reloading the page it worksphp flush() 有时不起作用,重新加载它工作的页面
【发布时间】:2015-05-04 12:30:01
【问题描述】:

我不明白我的代码中的问题,我想它与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

【问题讨论】:

    标签: php flush


    【解决方案1】:

    这可能是原因,来自php.net

    如果你调用 ob_flush() 和 flush() 仍然没有得到缓冲区 刷新可能是因为某些杀毒软件(熊猫在这个 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>';
            }
        }
    ?>
    

    【讨论】:

    • 我必须把它放在Index.php、Class.php或两者的开头?
    • index.php(包括class.php之前)
    • 好的,我会在多次尝试后告诉你它是否有效?谢谢你的帮助:)
    • 对不起,我尝试了很多次,但似乎没有解决我的问题...有时我有错误...它让我发疯..我整天都在尝试修复它...
    • 请您告诉我如何自动模拟页面重新加载?我真的需要让它以任何方式工作,拜托
    猜你喜欢
    • 2017-11-11
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多