Command line PHP output to file

How can I get the output of a PHP program run in the command line in DOS to be saved to a file instead of or in addition to printed in the DOS terminal?

Regardless of how you run your php script - from command line or from web, you can save output generated by your script using output control functions. Here is example:


<?php ob_start(); echo '

Hello World!

'; ?>

Some html output here...

<?php $content = ob_get_contents(); ob_end_clean(); $fp = fopen('myfile.txt','w'); if($fp){ fputs($fp,$content,strlen($content)); } fclose($fp); ?>


If, in addition to saving content to a file you need to output it to browser or terminal, you can add this to the code above:

echo $content;

Sponsor our Newsletter | Privacy Policy | Terms of Service