Last active
March 6, 2020 06:48
-
-
Save sohelrana820/5524b9617182b4b3d30cc03151063a10 to your computer and use it in GitHub Desktop.
How to Recursively Delete a Directory and its Entire Contents Using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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