fclose() expects parameter 1 to be resource boolean given

Discuss everything related to Business Directory. All versions. Any questions related to the Business Directory developing or using should be posted to this forum.

Moderator: alexandrleonenko

chirng
Master
Master
Posts: 149
Joined: Jun 7th, '09, 04:15

fclose() expects parameter 1 to be resource boolean given

Postby chirng » Apr 17th, '18, 06:29

framework/helpers/CFile.php line 341:-

public static function writeToFile($file = '', $content = '', $mode = 'w')
{
$fp = @fopen($file, $mode);
@fwrite($fp, $content);
@fclose($fp); // line 341
self::_errorHanler('file-writing-error', A::t('core', 'An error occurred while writing to file {file}.', array('{file}'=>$file)));
return true;
}

I change some scripts to overcome the problem as below:-

public static function writeToFile($file = '', $content = '', $mode = 'w')
{
$fp = @fopen($file, $mode);
@fwrite($fp, $content);
if($fp === false){
return true;
}
else
@fclose($fp);
self::_errorHanler('file-writing-error', A::t('core', 'An error occurred while writing to file {file}.', array('{file}'=>$file)));
return true;
}

Please advise,

TQ

administrator
Site Admin
Site Admin
Posts: 6003
Joined: Jan 7th, '09, 23:18
Contact:

Re: fclose() expects parameter 1 to be resource boolean given

Postby administrator » Apr 18th, '18, 09:03

Rewrite it in following way:

Code: Select all

public static function writeToFile($file = '', $content = '', $mode = 'w')
{
   if(!$fp = @fopen($file, $mode)){
      self::_errorHanler('file-opening-error', A::t('core', 'Cannot open file {file}.', array('{file}'=>$file)));
      return false;
   }      
   if(@fwrite($fp, $content) === FALSE){
      self::_errorHanler('file-writing-error', A::t('core', 'An error occurred while writing to file {file}.', array('{file}'=>$file)));
      return false;
   }
   
   @fclose($fp);
   return true;
}



Return to “ApPHP Business Directory / uBusinessDirectory Talk {developers/users}”