Home  >  Article  >  Backend Development  >  A general method to implement recursive deletion of directories in PHP

A general method to implement recursive deletion of directories in PHP

WBOY
WBOYOriginal
2016-07-25 08:45:29996browse
  1. /**
  2. *
  3. +------------------------------------------------- -----------------------
  4. * Description Recursively delete directories
  5. +-------------------------- ------------------------------------------------
  6. * @param string $dir The directory to be deleted
  7. +--------------------------------------------- --------------------------------
  8. * @return If the directory does not exist or is deleted successfully, it will return TRUE
  9. +--- -------------------------------------------------- ---------------
  10. * @author gongwen
  11. +---------------------------- ----------------------------------------
  12. */
  13. function rmdirs($dir){
  14. if (!is_dir($dir) || rmdir($dir)) return TRUE;
  15. if($dir_handle=opendir($dir)){
  16. while($filename=readdir($dir_handle)){
  17. if($filename!='.' && $filename!='..'){
  18. $subFile=$dir.'/'.$filename;
  19. }
  20. is_dir($subFile)?rmdirs($subFile):unlink($subFile);
  21. }
  22. closedir($dir_handle);
  23. return rmdir($dir);
  24. }
  25. }
复制代码

PHP


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