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

发布于 2024-09-01 18:33:26 字数 1589 浏览 2 评论 0原文

我希望使用 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 循环中,因为我可以监视数据库并且正在插入记录。

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

I am using ob_start()/ob_flush() to, hopefully, give me some progress during a long import operation.

Here is a simple outline of what I'm doing:

<?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();

However, nothing is being output to the screen at all.

I know the data file is getting downloaded because I watch the directory where it is being placed.

I also know that the import is happening, meaning that it is in the while loop, because I can monitor the DB and records are being inserted.

Any ideas as to why I am not getting output to the screen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

吹梦到西洲 2024-09-08 18:33:26

您还需要检查 PHP 设置,

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

output_buffering = 关闭
输出缓冲 = 4096

同意乔治的观点,但请检查上述设置

You also need to check the PHP settings

some installs default to 4096, some default to off

output_buffering = Off
output_buffering = 4096

agreed with George but do check the above settings

皓月长歌 2024-09-08 18:33:26

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

print ob_get_level ();

ob_start()之前;如果会看到其他东西,那么 0 你就得到了答案。

Make sure that your output buffering doesn't start automatically. Run:

print ob_get_level ();

before ob_start (); if will will see something else then 0 you've got the answer.

掀纱窥君容 2024-09-08 18:33:26

嘿伙计我也陷入了这个问题
并最终得到了正确的解决方案
这是给你的,

你必须为你的页面添加内容类型
你可以通过两种方式做到这一点
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' );
for($i=0;$i<70;$i++)
{
echo 'printing...<br>';
ob_flush();
flush();
sleep(3);
}
?>

一切顺利

Hey man I was also got stuck in this problem
and finally got the correct solution
here it is for you

you have to add content type for your page
you can do that by two ways
1. using html tag

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

Ex.

<!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. using php header function

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

Ex.

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

All the best

你曾走过我的故事 2024-09-08 18:33:26

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

Ob_end_clean() discards the contents of the current output buffer and turns off the buffering.
You should use ob_end_flush() instead.

浪漫之都 2024-09-08 18:33:26

添加这一行

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

对我有用。

Add this line

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

worked for me.

信愁 2024-09-08 18:33:26

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

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

php_value output_buffering Off
php_value output_buffering 4096

为我工作。谢谢你!

检查此网站: 点击此处

You can edit it with the .htaccess file

To disable output buffering, modify the line as follows:

php_value output_buffering Off
php_value output_buffering 4096

worked for me. Thank you!

Check this site: Click Here

青衫负雪 2024-09-08 18:33:26

您的网络服务器可能正在进行自己的缓冲。可能是像 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 代理/防火墙后面查看页面,该防火墙在提供页面之前会等待整个页面(以便它可以扫描它以查看它是否看起来像例如色情作品)。

It's possible that your webserver is doing its own buffering. Probably with something like mod_gzip.

Here is some very simple test code:

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

If it takes 10 seconds for that page to load, rather than seeing a new line every 2 seconds, then it means that it is being cached by your webserver. For what you are trying to do, there is no need to use ob_start and ob_flush. Just call flush whenever you want to force the content to the browser. However, like I mentioned, if the webserver is waiting for the content to complete before sending, then that won't do anything for you.

Edit: Another possibility is that you're viewing the page from behind a corporate or ISP proxy/firewall that waits for the whole page before serving it (so that it can scan it to see if it looks like pornography, for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文