String not saved into file ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
czy11421
Forum Newbie
Posts: 7
Joined: Sat Dec 26, 2009 9:03 pm

String not saved into file ?

Post 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>';
    }
}
 
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: String not saved into file ?

Post 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.
Post Reply