Page 1 of 1

String not saved into file ?

Posted: Fri Jan 08, 2010 9:02 am
by czy11421
why this code doesn't work ? it will echo result, but the String is NOT saved into file "sys.log" .

The echo result is

Code: Select all

 
log Resource id #5
result saved : 31
close file : 1
 

Code: Select all

 
class LogUtil {
 
    static function log($msg) {
 
        $filename = "sys.log";
 
        // open file
 
        ($fd = fopen($filename, "a") ) or die ("Couldn't open file, sorry");
 
        date_default_timezone_set('America/New_York');
 
        // append date/time to message
        $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
 
        echo 'log '.$fd.'<br>';
 
        // write string
        $rtn = fwrite($fd, $str . "\n");
 
        echo 'result saved : '.$rtn.'<br>';
 
        // close file
        $rtn = fclose($fd);
 
        echo 'close file : '.$rtn.'<br>';
    }
}
 

Re: String not saved into file ?

Posted: Fri Jan 08, 2010 9:37 am
by Charles256
According to your output it should be working so not sure what to tell you other than to make sure you're code has permission to write to that file. Also maybe give http://us.php.net/file_put_contents a try. I normally use that unless need dictates otherwise which in your case it doesn't seem to.