长PHP脚本和回声/打印

I have some PHP scripts that can be called either from the command line or as a webpage (where the arguments are passed from other web pages using $GET or $POST).

They can take a while to execute, let’s say 5 minutes.

The scripts include some “echo” & “print” calls which allow me to know what is happening during the execution in real time.

The problem is that, in webpage mode, those echo calls don’t print anything in the browser till the end of the script execution. Or sometimes, half the echos appears after 2 minutes and the rest at the end.

Is there a simple way to make my print()/echo() calls appear in real time when my script are called in “webpage mode”?

Thanks in advance.

flush() may or may not work depending on the browser and size of the output (see: PHP Flush() not working in Chrome)

Apache can also buffer output if mod_gzip is enabled.

Your best bet is to log into a db/session/fs and have JS on client side polling for updates.

Use ob_flush() to force output to be sent to the browser before script execution completes.

I assume you are not using output buffering as your script outputs fine on consolde. Therefore use flush() to explicitely tell PHP it should send output to the browser.

I would suggest a flush every xxx outputs instead of flushing after every echo or print if they appear in short intervals.