diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index a587a7cf..241d5459 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -370,7 +370,8 @@ public function deletePath(string $path): bool foreach ($files as $file) { if (is_dir($file)) { - $this->deletePath(\substr_replace($file, '', 0, \strlen($this->getRoot().DIRECTORY_SEPARATOR))); + $relativePath = \substr($file, \strlen($this->getRoot().DIRECTORY_SEPARATOR)); + $this->deletePath($relativePath); } else { $this->delete($file, true); } diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index c3d91b23..09d14df9 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -376,6 +376,35 @@ public function testDeletePath() $this->assertEquals(false, $this->object->exists($path)); $this->assertEquals(false, $this->object->exists($path2)); $this->assertEquals(false, $this->object->exists($path3)); + + // Test Nested Directory Structure (prevents "Directory not empty" errors) + $nestedBucket = 'nested-bucket'; + + // Create nested structure: nested-bucket/sub1/sub2/ with files at each level + $this->assertTrue($this->object->createDirectory($nestedBucket)); + $this->assertTrue($this->object->createDirectory($nestedBucket.DIRECTORY_SEPARATOR.'sub1')); + $this->assertTrue($this->object->createDirectory($nestedBucket.DIRECTORY_SEPARATOR.'sub1'.DIRECTORY_SEPARATOR.'sub2')); + + // Add files in nested directories + $rootFile = $this->object->getPath('file1.txt'); + $rootFile = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.$nestedBucket, $rootFile); + $this->assertTrue($this->object->write($rootFile, 'Content 1', 'text/plain')); + + $nestedFile = $this->object->getPath('file2.txt'); + $nestedFile = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.$nestedBucket.DIRECTORY_SEPARATOR.'sub1'.DIRECTORY_SEPARATOR.'sub2', $nestedFile); + $this->assertTrue($this->object->write($nestedFile, 'Content 2', 'text/plain')); + + // Verify files exist + $this->assertTrue($this->object->exists($rootFile)); + $this->assertTrue($this->object->exists($nestedFile)); + + // Delete entire nested structure - should work without "Directory not empty" error + $this->assertTrue($this->object->deletePath($nestedBucket)); + + // Verify everything is deleted + $this->assertFalse($this->object->exists($rootFile)); + $this->assertFalse($this->object->exists($nestedFile)); + $this->assertFalse(file_exists($this->object->getRoot().DIRECTORY_SEPARATOR.$nestedBucket)); } public function testGetFiles()