Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sohelrana820/5524b9617182b4b3d30cc03151063a10 to your computer and use it in GitHub Desktop.
Save sohelrana820/5524b9617182b4b3d30cc03151063a10 to your computer and use it in GitHub Desktop.
How to Recursively Delete a Directory and its Entire Contents Using PHP
<?php
/**
* @author: Sohel Rana <sohelrana820>
* @author URL: https://blog.sohelrana.me/
* @link: https://blog.sohelrana.me/recursively-delete-directory-entire-contents-using-php/
* @licence: MIT
*
* @param $directory
* @return bool
*/
public function recursiveRemoveDirectory($directory)
{
foreach (glob("{$directory}/*") as $object) {
if (is_dir($object)) {
recursiveRemoveDirectory($object);
} else {
unlink($object);
}
}
return rmdir($directory);
}
// Usages
recursiveRemoveDirectory('PATH_OF_DIRECTORY');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment