PHP custom function rrmdir to recursively delete directories and files

WBOY
Release: 2016-07-25 09:08:10
Original
736 people have browsed it
  1. /**
  2. Recursively delete a directory and all files under it
  3. func: rrmdir
  4. */
  5. function rrmdir($dir) {
  6. if (is_dir($dir)) {
  7. $objects = scandir($dir);
  8. foreach ($ objects as $object) {
  9. if ($object != “.” && $object != “..”) {
  10. if (filetype($dir.”/”.$object) == “dir”) rrmdir( $dir.”/”.$object); else unlink($dir.”/”.$object);
  11. }
  12. }
  13. reset($objects);
  14. }
  15. }
  16. ?>
Copy code

Attachment: 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().

  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.

  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. }
  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:

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

For more information, please refer to http://cn.php.net/rmdir.

>>>>



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template