PHP警告fclose()期望参数1是给定的资源布尔值 [英] php warning fclose() expects parameter 1 to be resource boolean given

查看:657
本文介绍了PHP警告fclose()期望参数1是给定的资源布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b


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

这就是 /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; (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;


$ b $ fh = fopen('/ var / log / bfd_log','r');

fseek($ fh,$ res [1]);

$ blocks = 0;

if(!$ fh){
echo错误!无法打开文件。
} 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错误!无法关闭文件。
}

print(bfd_blocks\\\
$ blocks);

?>

第40行 $ fh = fopen('/ var / log / bfd_log','r'); 我查看了 / var / log 目录,没有名为 bfd_log的文件,我不知道我是否需要自己创建它,或者是自动创建的。

任何人都可以帮我解决这个错误,谢谢提前。

解决方案

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

请确保在使用变量中的资源之前,返回资源的函数没有遇到麻烦。

  $ fh = fopen('/ var / log / bfd_log ','r'); 
//在其他函数使用这个变量之前检查fh
if(!$ fh){
echoError!Could not open the file。;
} else {

//执行资源$ fh
的任务fseek($ fh,$ res [1]);
[...]

$ lfh = fopen('/ etc / snmp / bfd-log-pos.stat','w');

//在执行其他代码块之前检查并使用此变量
if($ lfh)
{

//执行资源为$的任务lfh
$ pos = ftell($ fh);
fwrite($ lfh,$ timestamp,$ pos);
fclose($ lfh);
fclose($ fh);

[...]

} else {
// lfh error
}
}


$ b如果你在使用变量之前总是检查,那么你将不会再遇到这个错误。

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.

解决方案

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.

这篇关于PHP警告fclose()期望参数1是给定的资源布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆