1
10

dirclear

dirclear
SYNOPSIS

dirclear($dir='.')

DESCRIPTION

dirclear deletes all the files and all the directories which are in the directory $dir and all its subdirectories. dirclear empties the current directory by default.

If $dir isn't a directory, dirclear returns false.

CODE
  1. function dirclear($dir='.') {
  2.     if (!is_dir($dir)) {
  3.         return false;
  4.     }
  5.  
  6.     dirclearaux($dir);
  7.  
  8.     return true;
  9. }
  10.  
  11. function dirclearaux($dir) {
  12.     $handle = opendir($dir);
  13.     while (($file = readdir($handle)) !== false) {
  14.         if ($file == '.' || $file == '..') {
  15.             continue;
  16.         }
  17.         $filepath = $dir . DIRECTORY_SEPARATOR . $file;
  18.         if (is_dir($filepath)) {
  19.             dirclearaux($filepath);
  20.             rmdir($filepath);
  21.         }
  22.         else {
  23.             unlink($filepath);
  24.         }
  25.     }
  26.     closedir($handle);
  27. }
SEE ALSO

dircopy, dirlist

Comments

To add a comment, click here.