方法ob_start和ob_flush不起作用,为什么?

时间:2010-05-14 19:27:36

标签: php progress-bar ob-start

我正在使用ob_start() / ob_flush(),希望在漫长的导入操作中给我一些进展。

以下是我正在做的简单概述:

<?php
ob_start ();

echo "Connecting to download Inventory file.<br>";
$conn = ftp_connect($ftp_site) or die("Could not connect");

echo "Logging into site download Inventory file.<br>";
ftp_login($conn,$ftp_username,$ftp_password) or die("Bad login credentials for ". $ftp_site);

echo "Changing directory on download Inventory file.<br>";
ftp_chdir($conn,"INV") or die("could not change directory to INV");

//      connection, local, remote, type, resume
$localname = "INV"."_".date("m")."_".date('d').".csv";
echo "Downloading Inventory file to:".$localname."<br>";

ob_flush();
flush();
sleep(5);

if (ftp_get($conn,$localname,"INV.csv",FTP_ASCII)) 
{
    echo "New Inventory File Downloaded<br>";
    $datapath = $localname;
    ftp_close($conn);
} else {
    ftp_close($conn);
    die("There was a problem downloading the Inventory file.");      
}
ob_flush();
flush();
sleep(5);

$csvfile = fopen($datapath, "r"); // open csv file
$x = 1;
// skip the header line
$line = fgetcsv($csvfile);
$y = (feof($csvfile) ? 2 : 5);
while ((!$debug) ? (!feof($csvfile)) : $x <= $y) {
    $x++;
    $line = fgetcsv($csvfile);
    // do a lot of import stuff here with $line
    ob_flush();
    flush();
    sleep(1);
}

fclose($csvfile); // important: close the file
ob_end_clean();

但是,屏幕上根本没有输出任何内容。

我知道数据文件正在下载,因为我正在查看它所在的目录。

我也知道导入正在发生,这意味着它在while循环中,因为我可以监视数据库并且正在插入记录。

关于为什么我没有输出到屏幕的任何想法?

7 个答案:

答案 0 :(得分:17)

您还需要检查PHP设置

某些安装默认为4096,有些默认为关闭

  

output_buffering = Off
  output_buffering = 4096

同意乔治,但请检查以上设置

答案 1 :(得分:13)

确保输出缓冲不会自动启动。运行:

print ob_get_level ();
在ob_start()之前的

;如果你会看到别的东西然后0你就得到了答案。

答案 2 :(得分:6)

嘿,我也遇到了这个问题 最后得到了正确的解决方案 这是给你的

您必须为您的网页添加内容类型 你可以通过两种方式做到这一点  1.使用html标签

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

实施例

<!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. 使用php标头功能

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

  2. 实施例

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

    一切顺利

答案 3 :(得分:3)

Ob_end_clean()丢弃当前输出缓冲区的内容并关闭缓冲。 你应该使用ob_end_flush()代替。

答案 4 :(得分:0)

添加此行

header("X-Accel-Buffering: no");

为我工作。

答案 5 :(得分:0)

您可以使用.htaccess文件进行编辑

要禁用输出缓冲,请如下修改该行:

php_value output_buffering Off
php_value output_buffering 4096

为我工作。谢谢!

检查此站点:Click Here

答案 6 :(得分:-1)

您的网络服务器可能正在进行自己的缓冲。可能与mod_gzip类似。

这是一些非常简单的测试代码:

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

如果加载该页面需要10秒钟,而不是每2秒看一个新行,那么这意味着它正在被您的网络服务器缓存。对于您要做的事情,不需要使用ob_start和ob_flush。只要您想将内容强制到浏览器,只需致电flush即可。但是,正如我所提到的,如果网络服务器在发送之前等待内容完成,那么这对你没有任何作用。

编辑:另一种可能性是您在公司或ISP代理/防火墙后面查看页面,该页面在提供之前等待整个页面(以便它可以扫描它以查看是否它看起来像色情,例如)。

相关问题