Default output stream

Posted on 2005-04-04
Medium Priority
Last Modified: 2006-11-18
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 Chicago...so that file needs to be Chicago.txt.  Thank you.
Question by:ronpac
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 40

Expert Comment

by:Richard Quadling
ID: 13698288
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.

Accepted Solution

peyox earned 1200 total points
ID: 13698382
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);

Author Comment

ID: 13698514
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 800 total points
ID: 13704666
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.

Author Comment

ID: 13705898
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.
LVL 40

Expert Comment

by:Richard Quadling
ID: 13705912
Smarty does a VERY good templating/caching system.

Author Comment

ID: 13705923
Thanks RQuadling...I'll research Smarty for caching.

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question