Explore three methods for deleting folders in PHP. 1. copy the code using recursion: deleteDir ($ dir) {if (rmdir ($ dir) falseis_dir ($ dir) {if ($ dpopendir ($ dir )) {while ($ filereaddir ($ dp ))! False) {if (is_dir ($ fil
1. recursion
The 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
The 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
The 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;
}
Example code: deleteDir ($ dir) {if (rmdir ($ dir) = false is_dir ($ dir) {if ($ dp = opendir ($ dir )) {while ($ file = readdir ($ dp ))! = False) {if (is_dir ($ fil...