2009-12-14 97 views
0

代码工作正常,除了事实,这里有一个问题:我想你不追加到文件PHP的日志记录问题

//Log Events 
function logEvent($newinput) { 
    if ($newinput !== NULL) { 
     // Add a timestamp to the start of the $message 
     $newinput = date("Y/m/d H:i:s").': '.$newinput; 
     $fp = fopen('log.txt', 'w'); 
     fwrite($fp, $newinput."\n"); 
     fclose($fp); 
    } 
} 
//Problem writing these two lines to log.txt? 
//The bad, the two lines below are not on the log.txt 
logEvent('Selection'.$selections[$selection]); 
logEvent('Change' . $change. 'cents.'); 

//This line is written to a text file (log.txt), okay that's good. 
logEvent('Input' . $newinput); 
+0

对不起Newb,我应该注意到,当我第一次写这个函数。 – 2009-12-14 13:48:14

回答

1

,你重写它。试着用'a'而不是'w'打开。

+0

甜蜜,谢谢,只是想知道周围的人有没有睡过... – Newb 2009-12-14 13:42:26

+0

它可能是一天中的任何时间,你会得到比闪电更快的反应,那真棒。 – Newb 2009-12-14 13:43:20

+0

再次感谢ufk。 – Newb 2009-12-14 13:44:19

0

您需要使用追加修改打开文件时,你已经走了

的fopen( 'log.txt的', 'W');

这意味着每次调用该函数,日志文件是越来越吹出来并重新创建,如果用来代替

的fopen(“log.txt的”,“A”);

然后您的新日志条目将追加到该文件。

您也可以考虑让文件保持打开状态以便以后插入,但其他请求中可能存在多个更新的问题。