4

I'm working on a cron php script which will run once a day. Because it runs this way, the output from the file can't be seen.

I could literally write all the messages I want into a variable, appending constantly information I want to be written to file, but this would be very tedious and I have a hunch not necessary.

Is there a PHP command to tell the write buffer to write to a log file somewhere? Is there a way to get access to what has been sent to the buffer already so that I can see the messages my script makes.

For example lets say the script says

PHP:

<?
  echo 'hello there';
  echo 'hello world';
?>

It should output to a file saying: 'hello therehello world';

Any ideas? Is this possible?

I'm already aware of

file_put_contents('log.txt', 'some data', FILE_APPEND);

This is dependent upon 'some data', when I don't know what 'some data' is unless I put it in a variable. I'm trying to catch the results of whatever PHP has outputted.

NikiC
  • 100,734
  • 37
  • 191
  • 225
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154

3 Answers3

11

You may want to redirect your output in crontab:

php /path/to/php/file.php >> log.txt

Or use PHP with, for example, file_put_contents():

file_put_contents('log.txt', 'some data', FILE_APPEND);

If you want to capture all PHP output, then use ob_ function, like:

ob_start();
/*
We're doing stuff..
stuff
...
and again
*/
$content = ob_get_contents();
ob_end_clean(); //here, output is cleaned. You may want to flush it with ob_end_flush()
file_put_contents('log.txt', $content, FILE_APPEND);
Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • Here is the thing, 'some data' in your example would have to be a collection of all the outputs, is there a way to get access to whatever has currently been 'echoed' by PHP? – Joseph Astrahan Apr 01 '14 at 13:23
  • The crontab idea is good except, I don't have access to tell the crontab to write its results to file since I'm on a shared server. Why I was looking for this solution. – Joseph Astrahan Apr 01 '14 at 13:24
  • awesome, that is what I was looking for, thanks. Will accept answer soon, it's forcing me to wait 7 minutes. – Joseph Astrahan Apr 01 '14 at 13:26
1

you can use ob_start() to store script output into buffer. See php documentation ob_get_clean

  <?php

  ob_start();

  echo "Hello World";

  $out = ob_get_clean();
  $out = strtolower($out);

  var_dump($out);
  ?>
Adam Fischer
  • 1,075
  • 11
  • 23
  • ob_get_clean is useful if I need to print out the state of the PHP at different times since it clears the buffer for you, good to know +1 – Joseph Astrahan Apr 01 '14 at 13:35
0

If You're using cron I suppose that You run this on a Unix machine so:

One of approach is to write everything You want to stdout stream so in Unix You may grab this output to a file:

in php script:

$handle = fopen("php://stdout","w");

fwrite($handle,"Hello world"); // Hello world will be written to console

in cron job grab this output to a file:

@hourly php /var/www/phpscript.php >> /path/to/your/outputfile.txt

Notice: >> operator will append to a file and > operator will overwrite file by new data. File will be created automatically by first write

So everything you put to fwrite call as second argument will be placed in /path/to/your/outputfile.txt

You may call fwrite as many time as you want. Don't forget to close handler by fclose($handle);