1
14

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_link($filepath)) {
  19.             unlink($filepath);
  20.         }
  21.         else if (is_dir($filepath)) {
  22.             dirclearaux($filepath);
  23.             rmdir($filepath);
  24.         }
  25.         else {
  26.             unlink($filepath);
  27.         }
  28.     }
  29.     closedir($handle);
  30. }
SEE ALSO

dircopy, dirlist

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].