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
- function dirclear($dir='.') {
- if (!is_dir($dir)) {
- return false;
- }
- dirclearaux($dir);
- return true;
- }
- function dirclearaux($dir) {
- $handle = opendir($dir);
- while (($file = readdir($handle)) !== false) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- $filepath = $dir . DIRECTORY_SEPARATOR . $file;
- if (is_link($filepath)) {
- unlink($filepath);
- }
- else if (is_dir($filepath)) {
- dirclearaux($filepath);
- rmdir($filepath);
- }
- else {
- unlink($filepath);
- }
- }
- closedir($handle);
- }
Comments