5

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.

1
  • fopen may return false. So you have to check $lfh and $fh are not false before fclose()
    – hindmost
    Jul 30, 2014 at 9:19

3 Answers 3

4

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.

5
  • I added if($fh) before the fclose() function but doesn't solve the problem.
    – Keysho
    Jul 30, 2014 at 11:32
  • post your complete code the error message indicates to a line that is higher then your code above
    – ins0
    Jul 30, 2014 at 11:42
  • 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)
    – Keysho
    Jul 30, 2014 at 12:47
  • 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
    – ins0
    Jul 30, 2014 at 13:19
  • 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.
    – Keysho
    Jul 30, 2014 at 13:46
0

I wrestled with this problem and could not find the answer until I separated my write check (put it first) from the actual file write code. So before I would open the file fopen/fwrite then do the is_writable check and then do the fclose and i would get this error.
To resolve I moved the is_writable and variable declaration before the fopen/fwrite and the error went away. Shown below (former php code position shown in comments) The first comment did help me realize this... Thank you.

$myfile = "/var/www/html/newfile.txt";

if (is_writable($myfile))  {
  echo  "The file is writable";
}
else {
  echo "The file is not writable";
}

$txt = "$name, $email, $command, $searchtype,  $keyword \n";
$myfile = fopen('/var/www/html/newfile.txt', 'w') or die("Unable to open file!");
fwrite($myfile, $txt);

// $myfile = "/var/www/html/newfile.txt";

// if (is_writable($myfile))  {
//  echo  "The file is writable";
// }
// else {
//   echo "The file is not writable";
// }

fclose($myfile);
-2

Try

$fh = fopen('/var/log/bfd_log', 'a+');

a+ mode will create the file if it does not exists

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.