1. Recursion
Copy codeThe Code is as follows: 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 Call Method
Copy codeThe Code is as follows: function del_dir ($ dir)
{
If (strtoupper (substr (PHP_ OS, 0, 3) = 'win '){
$ Str = "rmdir/s/q". $ dir;
} Else {
$ Str = "rm-Rf". $ dir;
}
}
3. Circulation Method
Copy codeThe Code is as follows: function deltree ($ pathdir)
{
Echo $ pathdir; //
If (is_empty_dir ($ pathdir) // if it is empty
{
Rmdir ($ pathdir); // Delete directly
}
Else
{// Otherwise, read this directory, except
$ D = dir ($ pathdir );
While ($ a = $ d-> read ())
{
If (is_file ($ pathdir. '/'. $ a) & ($! = '.') & ($! = '..') {Unlink ($ pathdir. '/'. $ );}
// Delete a file directly
If (is_dir ($ pathdir. '/'. $ a) & ($! = '.') & ($! = '..'))
{// If it is a directory
If (! Is_empty_dir ($ pathdir. '/'. $ a) // whether it is null
{// If not, call itself, but the original path + its sub-directory name
Deltree ($ pathdir. '/'. $ );
}
If (is_empty_dir ($ pathdir. '/'. $ ))
{// Delete directly if it is null
Rmdir ($ pathdir. '/'. $ );
}
}
}
$ D-> close ();
Echo "all files in the directory must be deleted first"; //
}
}
Function is_empty_dir ($ pathdir)
{
// Check whether the directory is empty. Isn't my method good? It's just that there are other things except... and not empty.
$ D = opendir ($ pathdir );
$ I = 0;
While ($ a = readdir ($ d ))
{
$ I ++;
}
Closedir ($ d );
If ($ I> 2) {return false ;}
Else return true;
}