diff --git a/src/Image.php b/src/Image.php index 7d708b5..c917246 100644 --- a/src/Image.php +++ b/src/Image.php @@ -577,10 +577,14 @@ protected function _gc() // safety guard return; } + $suffixLength = strlen($this->_suffix); foreach (new DirectoryIterator($imgdir) as $file) { if (!$file->isDot() && !$file->isDir()) { if ($file->getMTime() < $expire) { - unlink($file->getPathname()); + // only deletes files ending with $this->_suffix + if (substr($file->getFilename(), -($suffixLength)) == $this->_suffix) { + unlink($file->getPathname()); + } } } } diff --git a/test/ImageTest.php b/test/ImageTest.php index 001d7ff..835b19c 100644 --- a/test/ImageTest.php +++ b/test/ImageTest.php @@ -219,6 +219,27 @@ public function testCaptchaImageCleanup() $this->assertFalse(file_exists($filename), "File $filename was found even after GC"); } + /** + * @group ZF-10006 + */ + public function testCaptchaImageCleanupOnlyCaptchaFilesIdentifiedByTheirSuffix() + { + $this->element->render($this->getView()); + $filename = $this->testDir."/".$this->captcha->getId().".png"; + $this->assertTrue(file_exists($filename)); + //Create other cache file + $otherFile = $this->testDir . "/zf10006.cache"; + file_put_contents($otherFile, ''); + $this->assertTrue(file_exists($otherFile)); + $this->captcha->setExpiration(1); + $this->captcha->setGcFreq(1); + sleep(2); + $this->captcha->generate(); + clearstatcache(); + $this->assertFalse(file_exists($filename), "File $filename was found even after GC"); + $this->assertTrue(file_exists($otherFile), "File $otherFile was not found after GC"); + } + public function testGenerateReturnsId() { $id = $this->captcha->generate();