Hi there,
At the beginning want to say thank you to author for great plugin!
At the second want to sorry that write in this topic, but I think no need to make new one cuz I have the same problem.
I have also faced with same problem (warning on line 59) on vps and fixed it with next code, that suppress warnings and attempt 10 tries to write to the file with random sleep from 50 to 250 ms if first try failed. Looks like errors have gone.
this ( autoptimizeCache.php on line 59 ):
file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX);
replace with this
$errorlevel = error_reporting();
error_reporting($errorlevel & ~E_WARNING);
for ($try=0; $try<10; $try++) {
if (file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX)) {
break;
} else {
usleep(rand(50, 250));
}
}
error_reporting($errorlevel);
You can also just ignore this warning in next way (without any sleep)
$errorlevel = error_reporting();
error_reporting($errorlevel & ~E_WARNING);
file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX);
error_reporting($errorlevel);
in this case the code just ignore warning that generated by the file_put_contents and keep logs clean
the another, but not recomended way is to put “@” at the beginning of this function:
@file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX);
And the best way, is to replace file_put_contents with class method that will use flock to make sure that code can set lock before made any operations. Something like this:
protected static function write_file($filepath, $content) {
if (!is_writable($filepath)) thrown new Exception('File '.$filepath.' is not writable');
$fp = @fopen($filepath, 'w');
if (!is_resource($fp)) thrown new Exception('Can\'t open file '.$filepath);
$lock = flock($fp, LOCK_EX)
if (!$lock) {
fclose($fp);
thrown new Exception('Can\'t lock file '.$filepath);
}
if (fwrite($fp, $content) === FALSE) {
thrown new Exception('Can\'t write to file '.$filepath);
}
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
and then in code
try {
self::write_file($filepath, $content);
} catch(Exception $ex) {
\\ oops something going wrong
\\ make decision what to do
}
P.S. This is information for the plugin author. If you user then think twice if you decide to apply this changes manually to the plugin code. At least it is bad idea, cuz all changes will be automatically overwritten after plugin updated.