Page 1 of 1

Output before end of execution of script

Posted: Sun Dec 04, 2005 5:48 pm
by phpCCore Brad
Hello,

This is the first time I have posted on this forum, but I have been searching for an answer to this problem for quite a bit of time and haven't found an answer that works. Anyhow, let me explain what I have created and what I need to add to it. I write a piece of software that and recently I was writing part of it that handles updates. Handling the updates was easy enough, but the problem I am now facing is: I send files, sometimes up to hundreds, between two servers using php. However, the process can sometimes take a few minutes. To a user it just appears like the website is loading the whole time, but in reality the website has been loaded for a while it is just uploading all those files. I have thought of two ways of fixing this.

1) Forking the script and having it do the uploads in the background so the user does not even see them.
2) Outputting after each file upload.

I would prefer to do #2, simply because then users can see what was uploaded and what wasn't. However, I can't figure out how to output before execution is finished on the script. I have a feeling that this can be accomplished with either headers, output buffering, or a combination of both. If anyone has any ideas on this I would greatly appreciated it.

Thanks!

Posted: Sun Dec 04, 2005 5:58 pm
by John Cartwright
If you want to show the output of your script in realtime, I would suggest you go with output buffering

Usefull Links: ob_flush, flush()

Or if you would like to see some working examples, search output buffering on these forums, as it has been discussed several times.. but it basically comes down to

1) initialzie your loop
2) flush the contents to browser
3) end loop

Hmm

Posted: Sun Dec 04, 2005 6:04 pm
by phpCCore Brad
I was messing around with it using sleep and a test document and it still wouldn't use real time. I did a script that outputted used sleep(10) and then outputed again with flush and ob_start and all still did not output until after everything.

Posted: Sun Dec 04, 2005 6:09 pm
by John Cartwright

Code: Select all

for ($x = 0; $x < 10; $x++) {
		 echo $x;
		 sleep(1); //Added a sleep command to the loop here
		 ob_flush();
		 flush();
	}
works fine for me :wink:

Make sure you call ob_flush() and flush() in that order.

Posted: Sun Dec 04, 2005 6:22 pm
by phpCCore Brad
I swear I tried that. *whistles* Anyhow thanks for your help! I really appreciate it.

Posted: Sun Dec 04, 2005 6:25 pm
by josh
Also make sure output buffering is disabled in php.ini, if it is turned on it will override your ob_implicit_flush calls and maybe override flush() (not sure about the latter)

Posted: Mon Dec 05, 2005 5:25 am
by foobar
What I found on win32 is that you need both ob_flush() and flush() executed consecutively. Also, before you output the main bulk of the page (or the first piece of output), do a str_repeat(' ', 300) to make sure there are at least 300 bytes sent to the browser. Some browsers such as Netscape only start outputting after receiving 256 bytes of data. If your first output is larger than that for sure, this shouldn't be a problem.

Posted: Sat Mar 18, 2006 10:53 pm
by phpCCore Brad
I have run into a problem with this again. We switched our servers, which means a new php installation. The output in real time no longer works. The only output settings I could find in the ini are set to the same as our old server. I cannot figure out what is different between this. Any help would be great!


Thanks,
Brad Bridges