How to Remove Directories Recursively with PHP - updated method


/ Published in: PHP
Save to your folder(s)

This function removes a directory and its contents.
Use with careful, no undo!


Copy this code and paste it in your HTML
  1. function rmdir_recursive($dir){
  2. $files = scandir($dir);
  3. foreach ($files as $file) {
  4. if ($file == '.' OR $file == '..') continue;
  5. $file = "$dir/$file";
  6. if (is_dir($file)) {
  7. rmdir_recursive($file);
  8. rmdir($file);
  9. } else {
  10. unlink($file);
  11. }
  12. }
  13. rmdir($dir);
  14. }

URL: http://bsd-noobz.com/blog/how-to-remove-directories-recursively-with-php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.