printf() to a file 
Author Message
 printf() to a file

I've a application that generates html on the fly - the normal way of
doing things.  However I need it to also write to a file on the server
- in a protected directory, for archival purposes.  Q. can the
printf() function be directed to write to a file, after using fopen()
of course, or using a Unix '>> outfile' construct.  Or printf() to a
stringVariable then I could fputs() it to the file.

thanks
mike

--
 Michael Vore, W3CCV       M-ASA [Ka8 (MU)]    WHIRL (Burley LIMBO)
      http://www.*-*-*.com/ ; <-Custom Woodworking
      http://www.*-*-*.com/ ;-The weblog



Tue, 26 Apr 2005 13:07:05 GMT  
 printf() to a file


Quote:
> [...] Q. can the
> printf() function be directed to write to a file, after using fopen()
> of course, or using a Unix '>> outfile' construct.  Or printf() to a
> stringVariable then I could fputs() it to the file.

Same as printf(), only it returns the string rather than printing it:
<http://www.php.net/manual/en/function.sprintf.php>.

So you can do something like:

...
$str = sprintf('This post worth %01.2f', 0.01);
fwrite($fp, $str);
...

HTH,
--
steven vasilogianis



Tue, 26 Apr 2005 12:14:09 GMT  
 printf() to a file

Quote:
> I've a application that generates html on the fly - the normal way of
> doing things.  However I need it to also write to a file on the server
> - in a protected directory, for archival purposes.  Q. can the
> printf() function be directed to write to a file, after using fopen()
> of course, or using a Unix '>> outfile' construct.  Or printf() to a
> stringVariable then I could fputs() it to the file.

Consider also output buffering.
http://www.php.net/manual/en/function.ob-start.php

ob_start (); // before any page generation

// output html on the fly

$content = ob_get_contents(); // store html in a variable

ob_end_flush(); // flush the contents to the browser

$fp = fopen('somefile.bak', 'w');
fwrite($fp, $content);
fclose($fp);

The above code should store a duplicate of the file served to the browser.
(untested)

regards,
reggie.



Tue, 26 Apr 2005 16:12:53 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. printf

2. Problem with printf

3. printf and locale settings

4. printing array elements using printf

5. printf inner part of a string

6. printf ?

7. PRINTF external variable

8. printf

9. printf question

10. Return value of printf?

11. printf question

12. printf: want trailing zeros gone

 

 
Powered by phpBB® Forum Software