Skip to content

Instantly share code, notes, and snippets.

@gabyfle
Last active May 2, 2019 18:56
Show Gist options
  • Save gabyfle/d7e74e70da6efbeab1c3b2f9636dc178 to your computer and use it in GitHub Desktop.
Save gabyfle/d7e74e70da6efbeab1c3b2f9636dc178 to your computer and use it in GitHub Desktop.
Deletes recursively a directory from the server's disk
<?php
/**
* Delete recursively a directory
* Gist : https://gist.github.com/Gabyfle/d7e74e70da6efbeab1c3b2f9636dc178
*
* @param string $path
*/
function delete_directory(string $path): void
{
$files = new \DirectoryIterator($path);
foreach ($files as $file) {
if ($file->isDot() === false) {
if($file->isFile()) {
unlink($file->getPathname());
} elseif ($file->isDir()) {
self::delete_directory($file->getPathname());
}
}
}
rmdir($path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment