Home  >  Article  >  Backend Development  >  PHP custom function rrmdir recursively deletes directories and files under directories

PHP custom function rrmdir recursively deletes directories and files under directories

WBOY
WBOYOriginal
2016-07-25 09:13:201137browse

Example, PHP custom function rrmdir.

  1. //Recursively delete directories and files
  2. function rrmdir($dir) {
  3. if (is_dir($dir)) {
  4. $objects = scandir($dir);
  5. foreach ($objects as $object) {
  6. if ($object != “.” && $object != “..”) {
  7. if (filetype($dir.”/”.$object) == “dir”) rrmdir($dir.”/ ”.$object); else unlink($dir.”/”.$object);
  8. }
  9. }
  10. reset($objects);
  11. }
  12. }
Copy code

rmdir (PHP 4, PHP 5) rmdir — delete a directory Report a bug Description bool rmdir ( string $dirname ) Attempts to delete the directory specified by dirname. The directory must be empty and must have appropriate permissions. Returns TRUE on success, or FALSE on failure. Note: Since PHP 5.0.0 rmdir() can also be used with certain URL wrapping protocols. See the list of Supported Protocols and Wrappers to see which URL wrapping protocols rmdir() supports. Note: Support for Context was added in PHP 5.0.0. See the Stream function for a description of context. Note: When safe mode is enabled, PHP will check when executing a script whether the directory being scripted has the same UID (owner) as the script being executed. See mkdir() and unlink(). example:

  1. function rrmdir($dir) {
  2. if (is_dir($dir)) {
  3. $objects = scandir($dir);
  4. foreach ($objects as $object) {
  5. if ($object != "." && $object != "..") {
  6. if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$ object); else unlink($dir."/".$object);
  7. }
  8. }
  9. reset($objects);
  10. rmdir($dir);
  11. }
  12. }
  13. ?>
Copy code

This isn't my code, but just thought I would share, since it took me so long to find. This is a function to delete a folder, all sub-folders, and files in one clean move. Just tell it what directory you want deleted, in relation to the page that this function is executed. Then set $empty = true if you want the folder just emptied, but not deleted. If you set $empty = false, or just simply leave it out, the given directory will be deleted, as well.

Example:

  1. function deleteAll($directory, $empty = false) {
  2. if(substr($directory,-1) == "/") {
  3. $directory = substr($directory, 0,-1);
  4. }
  5. if(!file_exists($directory) || !is_dir($directory)) {
  6. return false;
  7. } elseif(!is_readable($directory)) {
  8. return false;
  9. } else {
  10. $directoryHandle = opendir($directory);
  11. while ($contents = readdir($directoryHandle)) {
  12. if($contents != '.' && $contents != '..') {
  13. $path = $ directory . "/" . $contents;
  14. if(is_dir($path)) {
  15. deleteAll($path);
  16. } else {
  17. unlink($path);
  18. } // bbs.it-home.org
  19. }
  20. }
  21. closedir($directoryHandle);
  22. if($empty == false) {
  23. if(!rmdir($directory)) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. }
  30. ?>
Copy code

A patch to previous script to make sure rights for deletion is set: example:

  1. //Delete folder function
  2. //Recursively delete directories and files
  3. function deleteDirectory($dir) {
  4. if (!file_exists($dir)) return true;
  5. if (!is_dir ($dir) || is_link($dir)) return unlink($dir);
  6. foreach (scandir($dir) as $item) {
  7. if ($item == '.' || $item == '. .') continue;
  8. if (!deleteDirectory($dir . "/" . $item)) {
  9. chmod($dir . "/" . $item, 0777);
  10. if (!deleteDirectory($dir . "/ " . $item)) return false;
  11. };
  12. }
  13. return rmdir($dir);
  14. }
  15. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn