On 6 December 2010 20:47, Steve Staples <sstaples@xxxxxxxx> wrote: > On Mon, 2010-12-06 at 20:29 +0530, Ferdi wrote: > > Greetings List members, > > > > I have a script that takes quite a while to run, one or two hours, I wish > to > > redirect the normal php output to a file on the webserver itself. I don't > > mind if in the process, the browser displays a blank page. The reason I > want > > to do this is that if the script crashes or the browser Is closed by > > mistake, I have absolutely no record of where the script stopped running. > > > > I could use code like below > > At the beginning of the script: > > ob_start(); > > > > At the end of the script: > > $page = ob_get_contents(); > > ob_end_flush(); > > $fp = fopen("output.html","w"); > > fwrite($fp,$page); > > fclose($fp); > > > > However, I see some problems with this: > > I'm not too sure of the size of the output. It may balloon to over the > > buffering limit (in PHP? Apache?) and then what happens? > > Secondly, if the script crashes before the end, I won't get any output. > > Finally, I am using a library in the script that outputs status and error > > messages of its own. So, if I manually opened a file and used fwrite() > > alongside echo for my messages, I would lose out on those messages. > > > > Anybody has any pointers on how you could send the output not only to a > > browser, but also to a file on the webserver? If not, at least to a file? > > > > Thanks and regards, > > Ferdi > > Just curious, but if it takes that long to run, why are you running it > from a browser? why not run it from the commandline, that way you dont > have to change your php.ini for the webserver (increasing the timeout, > memory limits, etc etc... you can change those for the CLI only? > > 2 hours is a long time to "hope" that the browser doesn't close, or > connectivity doesn't get interupted for even 1 microsecond... > > if the script has "breaks" in it, where it starts to do something else, > you can put in an email to yourself, to say "hey, we're HERE now" > > but i would look into running it from the CLI over the webserver, you > would be less likely to run into issues on something that takes that > amount of time to run. > > If you needed the output to be displayed on a webpage, you can write the > progress to a file, and then have a php webpage that reads the file, and > using ajax or whatever, refresh the content. > > good luck in your script, and if you still run it from the browser, and > need to output to a file, then i would continually be writing content to > that file, every time you do soemthing, or start another part of the > script so you know EXACTLY where you are, at all times... > > Steve > Hi Steve, Thanks for the tips. To answer your queries, I don't mind using CLI. How do I then ensure the messages, error or otherwise, output by the library I use, show up in the file I'm outputting to? Please note that I only make calls to the functions and object methods from this library and error or success messages are probably echo'd by the code from the library. I believe some context is in order. I am actually sending an email to a large number of our customers (around 10,000), each with the customer's name. My script does output a simple success or failure message. The library outputs more technical info. All I would like to know is which email got sent and which one failed. As you point out, I could write the success / failure messages to a file, but, I would also like to capture the messages output by the library. Thanks and regards, Ferdi