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

前端 未结 3 2008
感情败类
感情败类 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:45

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

提交回复
热议问题