Skip to content

Commit c46bd78

Browse files
fix: improve recursive delete to handle hidden files (#152) (#153)
1 parent cabf77f commit c46bd78

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/Storage/Device/Local.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,26 @@ public function move(string $source, string $target): bool
340340
public function delete(string $path, bool $recursive = false): bool
341341
{
342342
if (\is_dir($path) && $recursive) {
343-
$files = $this->getFiles($path);
343+
$entries = \scandir($path);
344344

345-
foreach ($files as $file) {
346-
$this->delete($file, true);
345+
if ($entries === false) {
346+
return false;
347347
}
348348

349-
\rmdir($path);
350-
} elseif (\is_file($path) || \is_link($path)) {
349+
foreach ($entries as $entry) {
350+
if ($entry === '.' || $entry === '..') {
351+
continue;
352+
}
353+
354+
if (! $this->delete($path.DIRECTORY_SEPARATOR.$entry, true)) {
355+
return false;
356+
}
357+
}
358+
359+
return \rmdir($path);
360+
}
361+
362+
if (\is_file($path) || \is_link($path)) {
351363
return \unlink($path);
352364
}
353365

tests/Storage/Device/LocalTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public function testDelete()
114114
$this->assertEquals(is_readable($this->object->getPath('text-for-delete.txt')), false);
115115
}
116116

117+
public function testRecursiveDeleteRemovesHiddenFiles()
118+
{
119+
$directory = $this->object->getPath('delete-hidden');
120+
121+
$this->assertTrue($this->object->createDirectory($directory));
122+
$this->assertTrue($this->object->write($directory.DIRECTORY_SEPARATOR.'.hidden', 'secret'));
123+
$this->assertTrue($this->object->write($directory.DIRECTORY_SEPARATOR.'visible', 'visible'));
124+
125+
$this->assertTrue($this->object->delete($directory, true));
126+
$this->assertFalse($this->object->exists($directory));
127+
}
128+
117129
public function testFileSize()
118130
{
119131
$this->assertEquals($this->object->getFileSize(__DIR__.'/../../resources/disk-a/kitten-1.jpg'), 599639);

0 commit comments

Comments
 (0)