Home  >  Article  >  Backend Development  >  PHP traverses and deletes entire directories and files

PHP traverses and deletes entire directories and files

WBOY
WBOYOriginal
2016-07-25 08:43:48884browse

We can use RecursiveDirectoryIterator and RecursiveIteratorIterator to delete directories, subdirectories and files. The subdirectories will be deleted with the parent directory first

  1. function cleanup_directory($dir) {
  2. $iter = new RecursiveDirectoryIterator($dir);
  3. foreach (new RecursiveIteratorIterator($iter, RecursiveIteratorIterator::CHILD_FIRST)
  4. as $f) {
  5. if ($f->isDir()) {
  6. rmdir($f->getPathname());
  7. } else {
  8. unlink($f->getPathname());
  9. }
  10. }
  11. rmdir($dir);
  12. }
  13. cleanup_directory('c:\wamp\junk');
  14. ?>
Copy code

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