Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make PHP progressively output as the script executes?

So I'm writing a disposable script for my own personal single use and I want to be able see how the process is going. Basically I'm processing a couple of thousand media releases and sending them to our new CMS.

So I don't hammer the CMS, I'm making the script sleep for a couple of seconds after every 5 requests.

I would like - as the script is executing - to be able to see my echos telling me the script is going to sleep or that the last transaction with the webservice was successful.

Is this possible in PHP?

Thanks for your help!

Iain

like image 521
Iain Fraser Avatar asked Mar 25 '10 04:03

Iain Fraser


2 Answers

Use ob_flush to send any data in the buffer. So you can execute some commands, flush the output, and then sleep for a bit before processing more commands.

I do notice that on particularly long scripts, PHP seems to start sending what it's got in the buffer even though the script hasn't finished executing

By default, PHP will never echo anything until the entire script finishes executing. That's just your browser trying to output way too much data at once, or you have something that's causing it to flush somewhere.

like image 86
animuson Avatar answered Nov 08 '22 01:11

animuson


Here's a PHP package which wraps it up :

https://packagist.org/packages/coderofsalvation/browser-stream

   BrowserStream::enable();
    BrowserStream::put("loading");

    for( $i = 0; $i < 10; $i++ ){
        BrowserStream::put(".");
        sleep(1);
    }
like image 39
coderofsalvation Avatar answered Nov 08 '22 02:11

coderofsalvation