2

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.

MikaelW
  • 1,145
  • 4
  • 11
  • 29

3 Answers3

3

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.

Community
  • 1
  • 1
Mahakala
  • 761
  • 1
  • 10
  • 16
  • Thanks! Yes, just tried those flush() functions and in my case they don't work. the browser, the server, etc might apparently decide not to show it before the end... Not sure the whole thing deserve to get into db/filesystem pipes/etc and javascript but maybe that is the only way? One would have thought that's a common problem with a common solution. – MikaelW Jan 29 '13 at 23:50
  • 1
    @MikaelW follow that gzip hint – hek2mgl Jan 30 '13 at 00:00
  • just tried with 2 different servers and the behaviour was the same. To be honest as I don't think i have control on the server property files, I think I'm a bit stuck as far as this approach is concerned. – MikaelW Jan 30 '13 at 00:18
  • After hacking a bit, I think thats the best answer. I got it working using AJAX but not in Chromium Browser – hek2mgl Jan 30 '13 at 00:45
1

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

Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
1

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.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Thanks! I just tried flush and ob_flush following your suggestion and another one. It didn't work. I'm reading the documentation of those functions right now and it doesn't sound good… Might be the server, the browser, etc… Might have to go for something quite different. – MikaelW Jan 29 '13 at 23:44
  • You'll only need flush(). I've done such things. (displaying in a progressbar) . Which browser are you using? – hek2mgl Jan 29 '13 at 23:46
  • Just tried this: for ($i = 0; $i < 10; $i++) { sleep(2); echo "YO!
    "; flush(); } and it didn't work with either safari or firefox or chrone
    – MikaelW Jan 29 '13 at 23:53
  • Thats the short example I like :) I will test it – hek2mgl Jan 29 '13 at 23:55