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

4

6

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

Zuniga answered 25/3, 2010 at 4:43 Comment(4)
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 - any way of hijacking this?Zuniga
Hmmmm... as an afterthought - perhaps PHP is actually sending the buffer as it goes and it's just my browser that's not showing it to me... ermmm.. help?!Zuniga
Are you running the script in a console? Or in a browser?Invoice
Thanks Carson, In the browser.Zuniga
M
11

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.

Mcconaghy answered 25/3, 2010 at 4:48 Comment(2)
PHP buffers the output, on long scripts this buffer becomes full so its data is sent out (you can adjust the size of the buffer or force php to buffer all output before sending)Phare
I'm giving this the tick because it lead me to the right answer. Anyone else doing this please note: You need to set output_buffering in your php.ini file to 0 - which is a performance hit I'm lead to beleive so it works for me - but I'd be skeptical about using it in production. Also, after calling ob_flush(), you also need to call flush() to sent the output. Hope this helps!Zuniga
L
3

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);
    }
Lines answered 14/8, 2015 at 17:34 Comment(0)
T
2

You can try using flush and look at the other output control functions, but they might not be any use. Your web server software may buffer the response regardless.

Tool answered 25/3, 2010 at 4:50 Comment(0)
B
0

You may want to explore the flush commands http://php.net/manual/en/function.flush.php

Brahe answered 25/3, 2010 at 4:46 Comment(1)
because the output is buffered and won't necessarily be output when the 'echo' statement is called.Invoice

© 2022 - 2024 — McMap. All rights reserved.