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

43,302

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.

Share:
43,302
Keysho
Author by

Keysho

Updated on March 20, 2020

Comments

  • Keysho
    Keysho about 4 years

    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 resource, boolean given Stack trace: in fclose called at /etc/snmp/bfd-stats.php (68)

    This is how /etc/snmp/bfd-stats.php looks like

    <?php
    
    $a = 0;
    $ptr = 0;
    $any = 0;
    $mx = 0;
    $ns = 0;
    $cname = 0;
    $soa = 0;
    $srv = 0;
    $aaaa = 0;
    $txt = 0;
    $total = 0;
    
    if(file_exists('/etc/snmp/bfd-log-pos.stat')) {
        $lfh = fopen('/etc/snmp/bfd-log-pos.stat','r');
        $string = fread($lfh,2087);
        $res = explode(',',$string);
        fclose($lfh);
    }
    else {
        $res = array();
        $res[0] = 0;
        $res[1] = 0;
    }
    
    if(file_exists("/var/log/bfd_log.1")) {
        $stats = stat('/var/log/bfd_log.1');
        if($stats[10] > $res[0]) {
            $res[0] = 0;
            $res[1] = 0;
        }
    }
    
    $fh = fopen('/var/log/bfd_log', 'r');
    
    fseek($fh,$res[1]);
    
    $blocks = 0;
    
    if(!$fh) {
        echo "Error! Couldn't open the file.";
    } else {
        while (!feof($fh)) {
            $data = fgets($fh);
            if(preg_match('/executed\sban/',$data)) {
                $blocks++;
            }
        }
    }
    
    $lfh = fopen('/etc/snmp/bfd-log-pos.stat','w');
    
    $timestamp = time();
    $pos = ftell($fh);
    fwrite($lfh,"$timestamp,$pos");
    fclose($lfh);
    
    if(!fclose($fh)) {
        echo "Error! Couldn't close the file.";
    } 
    
    print("bfd_blocks\n$blocks");
    
    ?>
    

    On line 40: $fh = fopen('/var/log/bfd_log', 'r'); I looked at the directory /var/log and there is no file called bfd_log, I dont know if I have to create it by myself or it is automatically created.

    Can anyone help me on fixing this error, Thanks in advance.

  • Keysho
    Keysho almost 10 years
    I added if($fh) before the fclose() function but doesn't solve the problem.
  • ins0
    ins0 almost 10 years
    post your complete code the error message indicates to a line that is higher then your code above
  • Keysho
    Keysho almost 10 years
    Thank you @ins0, You were right i tried to make sure if every variable is good to go before using it. Now every thing seems good, and i am having this obvious problem E_WARNING: fopen(/var/log/bfd_log): failed to open stream: No such file or directory; Do you think i have to create the file myself or force it to be created(but remember $lfh = fopen('/etc/snmp/bfd-log-pos.stat','r'); initially we open it just to read information)
  • ins0
    ins0 almost 10 years
    it depends on the task you are trying to solve, if you don't want to create files that not exists by fopen use the file_exists function before try to read a file if(file_exists..) { //do fopen etc } may accept this as your answer if this was you looking for your question
  • Keysho
    Keysho almost 10 years
    Thanks! @insO, you are a life saver. Finally what do you suggest about creating the file manually as you can see its system log file.