Save Browser Output To A File With PHP Output Buffering Functions

19th March 2008 - 5 minutes read time

The PHP output buffering functions provide a handy way of intercepting the contents of the buffer before it is sent to the browser. The output is whatever is sent to the browser whenever you print something off. PHP allows you to capture this output in a buffer before it is sent to the browser.

Output buffering is controlled by two mechanisms. The first is through the php.ini directive output_buffering, which is usually set to off. It can be turned on by setting this to either on, or the number of bytes that the buffer can take up. When this byte allocation is full the output is sent to the browser.

The second mechanism is through the use of the ob_start() function, which turns on output buffering for the script it is being run on. If output buffering has been set in the php.ini file then this isn't needed, but if it has been set to 'off' then you will get some errors if you try to do anything with the buffer. To start output buffering just call the ob_start() function.

ob_start();

Whatever you try to print off now will not be output to the browser until the end of the script. To explicitly push the output to the browser you can call the ob_flush() function.

  1. echo 'test';
  2. ob_flush();

You can turn off output buffering at any time by calling either ob_end_clean() or ob_end_flush(). ob_end_clean() will turn off the top most buffer (you can have more than one) and discard the contents. ob_end_flush() will output the contents of the top most buffer then turn it off. Here is an easy way to use ob_end_flush() to flush and turn off all available output buffers and output their contents.

while (@ob_end_flush());

You can get the contents of the output buffer at any time (as long as you haven't turned it off) by calling the ob_get_contents() function. This returns the current buffer contents. Here is an example of it in action.

  1. <?php
  2. ob_start();
  3. echo 'test ';
  4. ?>
  5. some more text
  6. <?php
  7. $out = ob_get_contents();
  8. ob_end_flush();
  9. // $out now contains 'test some more text'.
  10. ?>

The ob_flush() function will empty the current output buffer and send this to the browser. So it is important to call this function after you have got the contents of the buffer. If you call it before you try to get the buffer contents there will be nothing there.

Using the ob_get_contents() function is how you can output text to both a file and the browser. The following example will write some text to the browser and also store this text in a variable that is written to a file at the end of the script. The file is given the current time stamp as a name. Make sure you have sufficient rights to write into this directory using chmod.

  1. $out = '';
  2. ob_start();
  3. echo 'test';
  4. $out .= ob_get_contents();
  5. ob_flush();
  6. echo ' some more text to add to the buffer';
  7. $out .= ob_get_contents();
  8. ob_end_flush();
  9. // check that something was actually written to the buffer
  10. if (strlen($out) > 0) {
  11. $file = 'debug/' . time() . '.html';
  12. touch($file);
  13. $fh = fopen($file, 'w');
  14. fwrite($fh, $out);
  15. fclose($fh);
  16. }

The ob_start() function can be called without any parameters, but you can supply the name of a function that will be used to pass the contents of the buffer through before it is sent to the browser. Using this mechanism is slightly easier as it can be easily removed from any script. The call back function must have a single string parameter, which is the contents of the buffer, and must return a string, preferably the contents of the buffer.

  1. function bufferCallBack($buffer)
  2. {
  3. // check that something was actually written to the buffer
  4. if (strlen($buffer) > 0) {
  5. $file = 'debug/' . time() . '.html';
  6. touch($file);
  7. $fh = fopen($file, 'w');
  8. fwrite($fh, $buffer);
  9. fclose($fh);
  10. }
  11. return $buffer;
  12. }
  13.  
  14. ob_start('bufferCallBack');
  15. echo 'test';
  16. echo ' some more text to add to the buffer';
  17. ob_end_flush();

Be warned that the use of output buffering can lead to the apparent slowdown of your website due to everything being generated server side before being sent to the client.

Update: Added some advice about having correct permissions and corrected a variable name in the second example. Thanks to William for his email on pointing this out :)

Comments

Permalink
thanks ! this helped me index 80000 webpages that little bit easier, I was making them into XML for a sitemap format. regards, paul

paul (Fri, 03/28/2008 - 10:47)

Permalink
Thanks man. Easy stuff but hard to find all in the same place with good examples

Jesse (Wed, 09/03/2008 - 02:15)

Permalink
A very good explaination, I've set this up to have dynamic filenames based on a list of possible dynamically created pages, it works perfectly.

rose (Thu, 03/21/2013 - 12:23)

Add new comment

The content of this field is kept private and will not be shown publicly.