1. Recursive method://No Test
The following are the referenced contents:
Deletedir ($dir)
{
if (RmDir ($dir) ==false && is_dir ($dir)) {
if ($DP = Opendir ($dir)) {
while (($file =readdir ($DP))!= false) {
if (Is_dir ($file) && $file!= '. ' && $file!= '. ') {
Deletedir ($file);
} else {
Unlink ($file);
}
}
Closedir ($DP);
} else {
Exit (' not permission ');
}
}
} |
2. System Invoke method
The following are the referenced contents:
function Del_dir ($dir)
{
if (Strtoupper (substr (php_os, 0, 3)) = = ' WIN ') {
$str = "rmdir/s/q". $dir;
} else {
$str = "Rm-rf". $dir;
}
} |
3. Cyclic method from webjx.com
The following are the referenced contents:
function deltree ($pathdir)
{
echo $pathdir;//I use it when I'm debugging.
if (Is_empty_dir ($pathdir))//If Empty
{
RmDir ($pathdir);//delete directly
}
Else
{//otherwise read this directory, except ... and. Outside
$d =dir ($pathdir);
while ($a = $d->read ())
{
if (Is_file ($pathdir. ') /'. $a) && ($a!= '. ') && ($a!= ' ... ')) {unlink ($pathdir. ') /'. $a);}
If it's a file, delete it directly.
if (Is_dir ($pathdir. ') /'. $a) && ($a!= '. ') && ($a!= ' ... '))
{//If it is a directory
if (!is_empty_dir ($pathdir. ') /'. $a))/is empty
{//If not, call itself, but the original path + his subordinate directory name
deltree ($pathdir. ' /'. $a);
}
if (Is_empty_dir ($pathdir. ') /'. $a))
{//If it is empty, delete it directly
RmDir ($pathdir. ' /'. $a);
}
}
}
$d->close ();
echo "must first delete all files under the directory";//I use it when I debug
}
}
function Is_empty_dir ($pathdir)
{
I don't think it's a good way to tell if the directory is empty. Just to see, except. Something else is not empty.
$d =opendir ($pathdir);
$i = 0;
while ($a =readdir ($d))
{
$i + +;
}
Closedir ($d);
if ($i >2) {return false;}
else return true;
} |