Output before end of execution of script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Output before end of execution of script

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
Last edited by John Cartwright on Sun Dec 04, 2005 6:10 pm, edited 3 times in total.
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Hmm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post by phpCCore Brad »

I swear I tried that. *whistles* Anyhow thanks for your help! I really appreciate it.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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)
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post 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
Post Reply