PHP fclose doesn't unlock the file when fopening it later

186 views Asked by At

My problem appeared when trying to log into a file. I made a cLog class with a ToLog method that works like this:

function ToLog($logType, $text) {


    $myDate = date("H:i:s d/m/Y");
    $ip = $_SERVER['REMOTE_ADDR'];

    $fichero = $this->GetLogFilename($logType);
    $filehandler = fopen($this->pathfichero.$fichero, "a");
    if ($filehandler == false)
    {
        print_r (error_get_last());
    }

    fputs($filehandler, $myDate.' | IP: '.$ip.' | USER: '.$_SESSION['usr'].' | '.$text."\r\n");         
    fclose($filehandler);

    return;

}

If I create an oLog() object from inside a method in some other class and call to $oLog->ToLog($logtype, 'what I want to log') it works just fine, but when I call to some other method which makes another call to the $oLog->ToLog(...) method, it fails and tells me that:

 [type] => 2 [message] => fopen(log.admin): failed to open stream: Permission denied [file] => /.../cLog.php

What is going on here? The first time I write on the file it works, the second time even though I have fclosed it, PHP tells me "permission denied". I suspect that somehow writing+closing the file is delayed, but how do I fix this then?

0

There are 0 answers