diff --git a/framework/caching/FileCache.php b/framework/caching/FileCache.php index d884f8097d4..919a05a5893 100644 --- a/framework/caching/FileCache.php +++ b/framework/caching/FileCache.php @@ -141,6 +141,12 @@ protected function setValue($key, $value, $duration) if ($this->directoryLevel > 0) { @FileHelper::createDirectory(dirname($cacheFile), $this->dirMode, true); } + // If ownership differs the touch call will fail, so we try to + // rebuild the file from scratch by deleting it first + // https://github.com/yiisoft/yii2/pull/16120 + if (is_file($cacheFile) && fileowner($cacheFile) !== posix_geteuid()) { + @unlink($cacheFile); + } if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) { if ($this->fileMode !== null) { @chmod($cacheFile, $this->fileMode);