Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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 Chicago...so that file needs to be Chicago.txt.  Thank you.
0
ronpac
Asked:
ronpac
  • 3
  • 3
2 Solutions
 
Richard QuadlingSenior Software DeverloperCommented:
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.

Regards,

Richard Quadling.
0
 
peyoxCommented:
You should use Output Control functions described here:
http://us4.php.net/manual/en/ref.outcontrol.php

Simple solution for you:

<?
      // start output buffering
      ob_start();

      // 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);
      fclose($f);
?>
0
 
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Richard QuadlingSenior Software DeverloperCommented:
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.

<?php

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.
0
 
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.
0
 
Richard QuadlingSenior Software DeverloperCommented:
Smarty does a VERY good templating/caching system.
0
 
ronpacAuthor Commented:
Thanks RQuadling...I'll research Smarty for caching.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now