【问题标题】:php buffer doesn't stop after ob_end_cleanphp 缓冲区在 ob_end_clean 之后不会停止
【发布时间】:2012-04-27 14:57:03
【问题描述】:

使用以下代码,我缓冲了一个布局文件。 在本地服务器上工作正常,但在实时服务器上它会一直缓冲直到 php 放弃。这导致旋转进度条和页面中所有 javascript 的延迟执行。看起来 ob_end_clean() 并没有真正退出缓冲。

ob_start();
require($layoutfile);
$return = ob_get_contents();
ob_end_clean();
return $return;

当我在刷新缓冲区后回显一个空格时,一切正常。但在这种情况下,我不会在屏幕上打印任何内容。

ob_start();
require($layoutfile);
$return = ob_get_contents();
if(ob_end_clean()) echo " ";
return $return;

有人遇到过这个吗?

【问题讨论】:

  • 你试过使用ob_flush吗?
  • @kuba with 'ob_flush' 它删除了 '$return' var
  • 很好奇。作为测试,用if (ob_get_level()) while (@ob_end_clean()); 替换ob_end_clean(); 是否允许脚本正常工作?
  • @webbiedave 不,这不起作用。它可能与服务器配置有关吗?在其他服务器和本地,它工作正常!
  • 我不确定。只有想到的事情是检查服务器上 PHP 版本的任何错误,尝试打开/关闭 Web 服务器中的压缩,检查 Apache SendBufferSize

标签: php buffering


【解决方案1】:

对于任何有缓冲问题的人:

试试echo ob_get_level() 看看你在哪一层。如果此函数返回的次数超过0,则您仍在缓冲区中。

要关闭所有层以防止出现问题,请执行以下操作:

while (@ob_end_clean()) {  
    // do nothing   
}

这将删除所有图层。

在此之后,您可以继续 - 即使只有 flush() 而不是 ob_flush()

您提到它可以在您的其他服务器上运行。您是否像here 一样激活了 gzip 压缩?

【讨论】:

    【解决方案2】:

    适合我的代码是:

    <?php
    
    include "../connect/connectdb.php";
    
    $txtIdCaso = $_GET['WWW_id_caso'];
    
    $sql = "SELECT * FROM caso_solicitud
            WHERE id_caso = '".$txtIdCaso."'";
    
    
    $result = mysqli_query($conn, $sql);
    $row    = mysqli_fetch_array($result);
    
    header('Content-Description: File Transfer');
    header('Content-type: "'.$row['tipo_archivo'].'"');
    header('Content-Disposition: attachment; filename="'.$row['nombre_archivo'].'"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public');
    header("Content-length: ".$row['longitud_archivo']);
    
    while (@ob_end_clean()) {  
    // do nothing   
    }
    
    die($row['doc_solicitud']);
    ?>
    

    希望能有所帮助。我测试了几种类型的格式,效果很好。

    您好。

    【讨论】:

      【解决方案3】:

      我猜ob_end_clean 之后有输出。使用 die/exit/header 可以正常工作(意味着 HTML 中不会有任何内容)。我刚刚遇到了类似的问题,但得到了解决方案。 输出缓冲处理输出到客户端,该函数会清除所有内容,直到执行。

      【讨论】:

        猜你喜欢
        • 2023-03-23
        • 1970-01-01
        • 2015-10-29
        • 1970-01-01
        • 2016-05-18
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        相关资源
        最近更新 更多