PHP deletes non-empty directory program code_PHP tutorial

WBOY
Release: 2016-07-13 17:14:52
Original
856 people have browsed it

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

// 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);
}
?>
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

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.html
TechArticle
A 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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!