2015-05-04 53 views
2

我不明白在我的代码中的问题,我想这是有关flush()
它有时可以工作,有时不会,但如果我重新加载页面,
(然后点击确认表单提交弹出框中的“继续”),它的工作原理!php 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 lines):

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 

回答

0

这可能是原因,从php.net

如果你调用使用ob_flush()和flush(),仍然没有得到缓冲 刷新这可能是因为一些杀毒软件(Panda在此 情况)持有缓冲区,直到页面在 发送给浏览器之前完成加载。

而且它似乎是重要的,包括在你的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>'; 
     } 
    } 
?> 
+0

我必须将它放在Index.php,Class.php或两者的开头? – neoDev

+0

index.php(之前包括class.php) – vivoconunxino

+0

好吧,我会告诉你经过多次尝试,如果它工作正常吗?感谢您的帮助:) – neoDev

相关问题