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

PHP traverses and deletes entire directories and files

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 08:43:48
Original
939 people have browsed it

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 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