Skip to content

Commit

Permalink
Add deleteDirectory method to DirHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhhui committed Mar 20, 2018
1 parent c54cc0a commit bb995ba
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Helper/DirHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,31 @@ public static function directoryIterator(

return new \RecursiveIteratorIterator($filterIterator);
}

/**
* @param string $path
* @return bool
*/
public static function deleteDirectory($path): bool
{
try {
$directoryIterator = new \DirectoryIterator($path);
foreach ($directoryIterator as $fileInfo) {
if ($fileInfo->valid() && $fileInfo->isExecutable()) {
if ($fileInfo->isDot()) {
continue;
} elseif ($fileInfo->isDir()) {
if (self::deleteDirectory($fileInfo->getPathname())) {
rmdir($fileInfo->getPathname());
}
} elseif ($fileInfo->isFile()) {
unlink($fileInfo->getPathname());
}
}
}
} catch (\Throwable $e) {
return false;
}
return true;
}
}

0 comments on commit bb995ba

Please sign in to comment.