Methods ob_start and ob_flush don't work, why?

ob_start performance
ob_start javascript
ob_start security
ob_start codeigniter
ob_start ajax
php ob_get_clean
nested ob_start php
ob_start stderr

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?


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

What is PHP Output Buffering?, , i.e. everything in the buffer. Usually, you'll assign this to a variable. 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.


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.

PHP, function before any echoing any HTML content in a script. Callback function: This is an optional parameter that expects a function that takes the contents of the output buffer and returns a string that is to be sent to the browser for rendering. Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.


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

What is an Output Buffer?, something, you are asking the content of template(generated before the call) to be sent as response. ob_flush() and flush are PHP’s built-in methods which are used to send the data to the upper layers. Buffered data is not sent to the upper layers unless the buffers are full or PHP code


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

What is flush() in PHP detail?, As of August 2012, all browsers seem to show an all-or-nothing approach to <?​php function flush_buffers(){ ob_end_flush(); ob_flush(); flush(); ob_start(); } ?> bool ob_start () Parameters : The function can accept a bunch of optional parameters as follows: Callback function: This is an optional parameter that expects a function that takes the contents of the output buffer and returns a string that is to be sent to the browser for rendering.


Add this line

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

worked for me.

ob_start - Manual, Should I use ob_start() at the beginning of every script that uses header('Location​: file.php') and Please don't reply to anymore of my posts. You've told him not to use that method and then in the next reply told him to do just that! I'm also in  ob_start is a PHP function which turns output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.


ob_flush - Manual, Buffers can be nested, so while one buffer is active, another ob_start() activates a new buffer. So ob_end_flush() and ob_flush() are not really sending the buffer to the output, but to the parent buffer. And only when there is no parent buffer, contents is sent to browser or terminal. I've tried several attempts at getting my flush and ob_flush to work. I've tried setting the ini to allow buffering, I've tried using several different functions I found online for output buffering


When to use ob_start() and ob_end_flush() - PHP, Don't send any output to the browser immediately, like you usually do. ob_flush​() outputs content from the buffer. Turn on output buffering // There will be no output until you "flush" or echo the buffer's contents ob_start();  Several sources are telling me that, when redirecting to another page output buffering needs to be on. I'm asking when is the best position in the code to use ob_start() and ob_end_flush().


Everything About Output Buffering in PHP » phpFashion, ob_end_clean(), ob_end_flush(), ob_clean(), ob_flush() and ob_start() may not be With ob callback: note that the second parameter sent to your method won't When using a callback with ob_start(), functions like ob_get_contents() don't  I have written a MVC-framework and I am using ob_start('error_handler') to be able to catch fatal errors aswell. This works great! In boot.php // Start the error-logging ob_start( 'error_logging' ); // Run the request Request::run( URL::getInstance()->init()->formatURL() ); // Flush and disable output-buffering ob_end_flush();