将PHP输出存储到文件?(Store PHP Output To File?)

编程入门 行业动态 更新时间:2024-10-27 04:28:53
将PHP输出存储到文件?(Store PHP Output To File?)

我正在研究一个每天运行一次的cron php脚本。 因为它以这种方式运行,所以文件的输出无法看到。

我可以将我想要的所有消息写入变量,不断追加我想要写入文件的信息,但这将非常繁琐,而且我没有必要预感。

是否有一个PHP命令告诉写入缓冲区写入某个日志文件? 有没有办法获得已经发送到缓冲区的内容,以便我可以看到我的脚本所做的消息。

比如说脚本说

PHP:

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

它应该输出到一个文件:'hello therehello world';

有任何想法吗? 这可能吗?

我已经知道了

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

这取决于“某些数据”,当我不知道“某些数据”是什么时,除非我把它放在一个变量中。 我试图捕捉PHP输出的结果。

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.

最满意答案

您可能希望在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);

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

更多推荐

本文发布于:2023-04-28 00:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   PHP   Store   File   Output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!