php 方法 ob_start 和 ob_flush 不起作用,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2837006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Methods ob_start and ob_flush don't work, why?
提问by MB34
I am using ob_start()/ob_flush()to, hopefully, give me some progress during a long import operation.
我正在使用ob_start()/ob_flush()以希望在长时间的导入操作中给我一些进展。
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.
我也知道导入正在发生,这意味着它在 while 循环中,因为我可以监视数据库和正在插入的记录。
Any ideas as to why I am not getting output to the screen?
关于为什么我没有输出到屏幕的任何想法?
回答by Geek Num 88
You also need to check the PHP settings
您还需要检查 PHP 设置
some installs default to 4096, some default to off
有些安装默认为 4096,有些默认为关闭
output_buffering = Off
output_buffering = 4096
输出缓冲 = 关闭输出
缓冲 = 4096
agreed with George but do check the above settings
同意乔治,但请检查上述设置
回答by Piotr Pankowski
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.
在 ob_start() 之前;if will will see something else then 0 你有答案。
回答by Rahul Shinde
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
你必须为你的页面添加内容类型你可以通过两种方式来做到这一点 1. 使用 html 标签
<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>
using php header function
<?php header( 'Content-type: text/html; charset=utf-8' ); ?>
使用php头函数
<?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
祝一切顺利
回答by fireweasel
Ob_end_clean() discards the contents of the current output buffer and turns off the buffering. You should use ob_end_flush() instead.
Ob_end_clean() 丢弃当前输出缓冲区的内容并关闭缓冲。您应该改用 ob_end_flush()。
回答by Majid Ramzani
Add this line
添加这一行
header("X-Accel-Buffering: no");
worked for me.
为我工作。
回答by Kip
It's possible that your webserver is doing its own buffering. Probably with something like mod_gzip.
您的网络服务器可能正在执行自己的缓冲。可能有类似 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 flushwhenever 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.
如果该页面加载需要 10 秒,而不是每 2 秒看到一个新行,那么这意味着它正在被您的网络服务器缓存。对于您要执行的操作,无需使用 ob_start 和 ob_flush。flush只要您想将内容强制发送到浏览器,就可以调用。但是,就像我提到的那样,如果网络服务器在发送之前等待内容完成,那么这对您没有任何作用。
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).
编辑:另一种可能性是您正在从公司或 ISP 代理/防火墙后面查看页面,在提供之前等待整个页面(以便它可以扫描它以查看它是否看起来像内容,例如)。

