Jump to content

How do you echo/flush strings during script execution?


ballhogjoni

Recommended Posts

I have looked at ob_start, ob_get_contents, etc.. but I can't get it to work. My code looks like this:

 

ob_start();
echo "starting";
ob_flush();
//do some stuff
echo "something else"
ob_flush();
//do some stuff
echo "something else"
ob_flush();

 

The echoes are only shown at the end of the script execution.

Link to comment
Share on other sites

It's ultimately up to the browser as to whether it shows the text as the page is downloading or not - IE hasn't always done so but the others typically do.

Remember to use sleep() so you actually have time to watch, and double-check that you don't have any kind of output buffering enabled by default (which it generally isn't).

echo "First echo
";
flush();

sleep(10);
echo "Second echo";

?>

Link to comment
Share on other sites

that doesn't work for me, i tried it in chrome and ff 5

 

I think the better question is, what is your ultimate goal with this process? Browsers choose not to display output buffer on their own and hold it to the end in an attempt to cache. Chrome is crazy with caching, I have not worked with FF5 much but I am sure it is heading that way as well.

 

If you let us know what your goal is, I think we will be able to help more.

Link to comment
Share on other sites

that doesn't work for me, i tried it in chrome and ff 5

 

I think the better question is, what is your ultimate goal with this process? Browsers choose not to display output buffer on their own and hold it to the end in an attempt to cache. Chrome is crazy with caching, I have not worked with FF5 much but I am sure it is heading that way as well.

 

If you let us know what your goal is, I think we will be able to help more.

 

I am on mac osx and my goal is to display to the user (my self) where I'm at within the script. My script runs for about 10 seconds do many things IE installing file, unzipping them, creating mysql tables, adding data to those tables, etc...

 

I would like to see that the script is uploading a file, then when it unzips it, I would like to see that. I would like to see when the script install the tables and then see when it adds data to the tables.

 

I hope that made sense.

Link to comment
Share on other sites

Output buffer is not ideal for that job, unfortunately. Since some browsers choose to ignore it and hold all output to the end.

 

What I would look into is using an AJAX type script to report you the status of how it is going, sort of like a "processing" script. That way you just keep the feed open and update the data on the page with ajax.

Link to comment
Share on other sites

If you have access to it use CLI (Command line interface) it will always output what you echo/print and it won't wait for a buffer. Just remember to use "\r\n" insted of "<br>".

 

on linux/mac: open terminal write: "php script.php"

 

on windows... i hate windows at thease instances but if you need help with it just ask and i will try and help you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.