A non-empty directory means that there are other files or folders in the directory. The deletion principle is: first we enter the directory to find out whether it is an empty directory or file and delete it. If not, then enter the lower-level directory until it is an empty directory and then , and then return to delete the upper level, that is, traverse the directory to delete.
This is a super simple PHP tutorial, it’s entry-level, so I won’t go into details
Code
The code is as follows
代码如下 |
复制代码 |
// 说明: 删除非空目录的解决方案
function removeDir($dirName)
{
if(! is_dir($dirName))
{
return false;
}
$handle = @opendir($dirName);
while(($file = @readdir($handle)) !== false)
{
if($file != '.' && $file != '..')
{
$dir = $dirName . '/' . $file;
is_dir($dir) ? removeDir($dir) : @unlink($dir);
}
}
closedir($handle);
return rmdir($dirName) ;
}
?>
|
|
Copy code
|
|
// Description: Solution to delete non-empty directories
function removeDir($dirName)
{
If(! is_dir($dirName))
{
return false;
}
$handle = @opendir($dirName);
While(($file = @readdir($handle)) !== false)
{
If($file != '.' && $file != '..')
{
$dir = $dirName . '/' . $file;
is_dir($dir) ? removeDir($dir) : @unlink($dir);
}
}
closedir($handle);
Return rmdir($dirName);
}
?>
There are many other methods, which will not be introduced here. In fact, unlink in the program deletes files, and rmdir deletes this directory.
http://www.bkjia.com/PHPjc/628900.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/628900.htmlA non-empty directory means that there are other files or folders in the directory. The deletion principle is: first we enter the directory Find out if it is an empty directory or file and delete it. If not, then enter the lower level...