Store PHP output to a file?

I am working on a cron php script that will run once a day. Since it works this way, output from the file is not possible.

I could literally write all the messages I want into a variable, adding constant information that I want to write to a file, but that would be very tedious and I didn’t have to.

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

For example, let's say the script says

PHP:

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

It should output a message to the file: "hello therehello world";

Any ideas? Is it possible?

I already know

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

" ", , " ", . , PHP.

+4
3

crontab:

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

PHP, , file_put_contents():

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

PHP, ob_, :

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);
+8

ob_start() script . . php ob_get_clean

  <?php

  ob_start();

  echo "Hello World";

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

  var_dump($out);
  ?>
+1

cron, , Unix, :

- , , stdout, Unix. :

php script:

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

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

cron :

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

. >> , > . ,

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

You can call fwrite as many times as you want. Remember to close the handler.fclose($handle);

0
source

Source: https://habr.com/ru/post/1534451/


All Articles