php warning fclose() expects parameter 1 to be resource boolean given

前端 未结 3 1980
感情败类
感情败类 2021-01-06 13:13

I use newrelic to keep track of anything on my website and I always get this error:

Error message: E_WARNING: fclose() expects parameter 1 to be resou

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 13:57

    The error indicates that you are trying to pass a variable with a boolean value (true/false) to a function that needs a resource instead of a boolean value.

    Please make sure that before you use resources from variables, the function that returns the resource has not run into trouble. Only on success perform the other functions that use this resource/variable.

    $fh = fopen('/var/log/bfd_log', 'r');
    // check fh before other functions use this variable
    if (!$fh) {
        echo "Error! Couldn't open the file.";
    } else {
    
        // perform task with resource $fh
        fseek($fh, $res[1]);
        [...]
    
        $lfh = fopen('/etc/snmp/bfd-log-pos.stat', 'w');
    
        // check before other code block is executed and use this variable
        if( $lfh )
        {
    
            // perform task with resource $lfh
            $pos = ftell($fh);
            fwrite($lfh, "$timestamp,$pos");
            fclose($lfh);
            fclose($fh);
    
           [...]
    
        } else {
            // lfh error   
        }
    }
    

    If you always check before using variables, you won't run into this error anymore.

提交回复
热议问题