Home > Backend Development > PHP Tutorial > PHP code to recursively delete all files in a directory and multi-level subdirectories

PHP code to recursively delete all files in a directory and multi-level subdirectories

WBOY
Release: 2016-07-25 09:04:36
Original
863 people have browsed it
  1. /**

  2. desc: recursively delete files in the directory
  3. link: bbs.it-home.org
  4. date: 2013/2/24
  5. */
  6. class cacheClearFile{
  7. var $dir = '111cn.Net';
  8. function __construct()
  9. {
  10. $this ->listFils();
  11. }

  12. function listFiles()
  13. {
  14. if(is_dir($this->dir))
  15. {
  16. if($dir_file=opendir($ this->dir))
  17. {
  18. while(($dir_list=readdir($dir_file))!==false)
  19. {
  20. if($dir_list!="." && $dir_list!="..")
  21. {
  22. if( is_file($dir_list)
  23. {
  24. unlink($dir_list);
  25. }
  26. else
  27. {
  28. $this->dir =$dir_list;
  29. listFils();
  30. }
  31. }
  32. }
  33. }else{
  34. echo("The directory cannot be opened");
  35. }
  36. }
  37. else
  38. {
  39. echo("Not a directory");
  40. }
  41. }
  42. }

  43. //Calling example

  44. $files = new cacheClearFile();
  45. $files->listFiles();
  46. ?>

Copy code


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