1

I have some PHP code that is receiving and processing large images. I'd like to echo out some JavaScript at certain points while the image is being processed to update the DOM with jQuery. Here is some sample code, but it isn't working. It just waits the entire 5 seconds and then makes the alerts happen back to back. I want it to do the first alert immediately and then next alert after 5 seconds.

ob_start();

echo '<script type="text/javascript">alert(\'1...\');</script>';

ob_flush();

sleep(5);

ob_start();

echo '<script type="text/javascript">alert(\'2...\');</script>';

ob_flush();

Can anyone help?

1
  • Your using output buffering, by definition, you can't display output while using output buffering. Am I wrong in this ? Commented Jun 11, 2011 at 0:08

4 Answers 4

1

Most browsers buffer content until a certain size is reached. Try making your script blocks longer by padding them with something.

Also: You should call flush, not just ob_flush, and make sure zlib compression is turned off.

Sign up to request clarification or add additional context in comments.

1 Comment

They should be called in this order: ob_flush(); flush();
1

I have some PHP code that is receiving and processing large images. I'd like to echo out some JavaScript at certain points while the image is being processed to update the DOM with jQuery.

This may be out-of-scope for what you have to get done, but I'd use AJAX for this. You can certainly get what you want to occur, but the approach isn't good in the long term.

Instead of submitting the whole page and waiting for it to come back at a crawl, use an AJAX request to upload the image and get the result. Then a timer on the client can issue separate AJAX "how far done are you?" requests. The two PHP instances would communicate via setting a "done" flag on the job entry in a database, etc.

While it makes the client-side stuff a bit more complex, it is much easier to handle user interaction (such as allowing the user to cancel a long-running job) and makes your PHP code a lot more tightly-focused.

Comments

0

Adding this to the top of the script will work:

for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

As far as I know, ob_implicit_flush(1) forces a flush on every output statement. So the other ob_start() and ob_flush() calls wouldn't be necessary. I don't know if that works for you.

<?php

for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

echo '<script type="text/javascript">alert(\'1...\');</script>';

sleep(5);

echo '<script type="text/javascript">alert(\'2...\');</script>';

?>

3 Comments

Ah crap I added that and it's not working for me. I'm on Firefox 4.
Try it without the ob_start() and ob_flush() calls (I added the complete code above). I'm using FF 3.4, and it's working alright for me. If that doesn't work, I wonder if we're on different versions of PHP?
Yeah I copied and pasted the code about with no luck. PHP 5.3.5 over here.
0

Following is working in FF4:

<?php
echo '<script type="text/javascript">alert("1");</script>';
flush();
sleep(5);
echo '<script type="text/javascript">alert("2");</script>';
?>

I implemented a chat "server" with something like that long ago. It was working.

This ob_* stuff isn't helpful for this.

2 Comments

No luck with this on FF4. T___T
For me it was working in FF4. Actually I used console.log() instead of alert() before. Maybe that was the problem? Now I changed that back alert().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.