views:

134

answers:

2

What is the best approach for setting up sequential output for a PHP installer script? Say I have a page, and want to display incremental output within a DIV provided by a PHP script.

So page.html might contain this:

<div id="output">
    <!-- Script Output Here -->
</div>

And install.php might look like this (for testing purposes):

<?php
// Turn off Output Buffering
ob_end_flush();

// Begin Test Output (5 second intervals)
echo 'Testing incremental output...' . "\n";
sleep(5);
echo 'Step 1...' . "\n";
sleep(5);
echo 'Step 2...' . "\n";
sleep(5);
echo 'Complete!' . "\n";

I was thinking AJAX might be the best approach, but I'm not sure how to approach that. If I load "install.php" via JavaScript, I won't get everything back until the script finishes execution (correct?) - making this entire goal moot. I've seen this done before, but can't find a concrete example of how it was done.

Any help appreciated!

+2  A: 

I doubt Ajax will do what you want - instead you may have to call the ajax fetches more frequently.

For flushing information out as soon as it's produced you need regular flush()'s after outputting information.

I've also added into a relevant .htaccess file a line to avoid any buffering due to GZipping the output as well (apache2).

SetEnvIf Request_URI "/url/dont/buffer/me.php" gzip=0

In the relevant PHP file:

ini_set('output_buffering', false);
ini_set('implicit_flush', 'true');

and the appropriate calls to flush(). That pretty much covers all the bases.

Alister Bulman
A: 

I would output everything to a text file and updating the DIV via AJAX by just calling the .txt file

Lucacri
Pretty hairy; multiple users? Then you've got multiple text files. Name the text file based on a session? What about multiple tabs? Oh crap I just divided by zero.
Tony k
Ah, screw it. Just time your install script and create an animated GIF. It's all the same to the user.
Calvin