php 警告 fclose() 期望参数 1 是给定的资源布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25032923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:36:55  来源:igfitidea点击:

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

phpfile-iowarnings

提问by Keysho

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

我使用 newrelic 来跟踪我网站上的任何内容,但总是收到此错误:

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)

错误消息:E_WARNING:fclose() 期望参数 1 是资源,给定的布尔值堆栈跟踪:在 /etc/snmp/bfd-stats.php (68) 处调用的 fclose

This is how /etc/snmp/bfd-stats.phplooks like

这是/etc/snmp/bfd-stats.php看起来像

<?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/logand there is no file called bfd_log, I dont know if I have to create it by myself or it is automatically created.

第40行:$fh = fopen('/var/log/bfd_log', 'r');我看了下目录/var/log没有叫的文件bfd_log,不知道是自己创建还是自动创建的。

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

谁能帮我解决这个错误,提前致谢。

采纳答案by ins0

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.

该错误表明您正在尝试将具有布尔值 (true/false) 的变量传递给需要资源而不是布尔值的函数。

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.

如果你总是在使用变量之前检查,你就不会再遇到这个错误了。

回答by guyzun

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.

我一直在努力解决这个问题,直到我将我的写检查(把它放在第一位)与实际的文件写代码分开后才能找到答案。所以在我打开文件 fopen/fwrite 之前,先做 is_writable 检查,然后再做 fclose,我会得到这个错误。
为了解决这个问题,我在 fopen/fwrite 之前移动了 is_writable 和变量声明,错误消失了。如下所示(以前的 php 代码位置显示在评论中)第一条评论确实帮助我意识到了这一点...谢谢。

$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);

回答by cocheese

Try

尝试

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

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

如果文件不存在,a+ 模式将创建文件