我正在研究一个每天运行一次的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.txtOr 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);更多推荐
发布评论