Default output stream

How do I change the default output stream in PHP? I'm caching output to file and want to change the default output stream to a file.  My script script will have a loop and I need to be able to change the output stream on the fly.  For example....

The first iteration might be New York, so the stream needs to be NewYork.txt so all output will go to this file...the next file might be that file needs to be Chicago.txt.  Thank you.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard QuadlingSenior Software DeveloperCommented:
Hi ronpac,

Either alter the script so no output is generated, but instead, a variable is written to and at the end of the script write it all out to the appropriate file. Or use output buffering.


Richard Quadling.
You should use Output Control functions described here:

Simple solution for you:

      // start output buffering

      // your output here
      echo "CHICAGO";

      // get the output to variable
      $output = ob_get_clean();

      // write to file
      $f = fopen ("chicago.txt", "w");
      fputs($f, $output);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ronpacAuthor Commented:
Oh, I like peyox example so far....simple enough. I think that's the way to go, I guess if I'm writing to multiple files, I'll have to close them, didn't think about that.

Okay, I'll keep this open for a bit longer but I think you got it.  The only thing that would be best (if possible) is to change the standard output on the file so that I wouldn't have to worry about the buffering, fputs, or fclose commands.  Again, peyonx makes a lot of sense and very simple.  I'll take a look at the Output Control functions.  Thank you.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Richard QuadlingSenior Software DeveloperCommented:
Output buffering is really a cheat. If the scripts intent is to write to files, then make it the scripts action also.

A nice thing about creating the output as a variable as you go is that you can output real time debugging messages.


function DoSomething()
echo "Doing something<br />\n"; // Debug
return 'Answer';

echo "Starting output generation<br />\n"; // Debug
$sFinalOutput = DoSomething();
echo "Finished output generation<br />\n"; // Debug

file_put_contents("output.txt", $sFinalOutput); // PHP 5 function of the PHP 4 3-line version.

Remove the lines with // Debug to remove debugs!

With output buffering enabled, you have no chance to catch errors in real time or do real time debugging. Also, any warnings or notices generated will be part of the output.
ronpacAuthor Commented:
Okay, I split the points.....I haven't implemented either of the solutions. I've requested these solutions to implement a caching system but at the moment it isn't needed....seems like a simple database index solved my index for now.  Thanks for your help and I'll have this for future reference when I start the caching system again.
Richard QuadlingSenior Software DeveloperCommented:
Smarty does a VERY good templating/caching system.
ronpacAuthor Commented:
Thanks RQuadling...I'll research Smarty for caching.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.