Home >Backend Development >PHP Tutorial >Implementation code for deleting non-empty directories in PHP_PHP tutorial

Implementation code for deleting non-empty directories in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:55:46898browse

The original method of PHP deleting directories and files is to first check whether there are files in this directory. If so, if it is a folder, then call this function to delete it. If it is a file, directly call unlink to delete it, and finally delete the directory.

When deleting a folder, you must first ensure that you have this permission!

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

removefunctionDir($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);
}
?>

http://www.bkjia.com/PHPjc/631656.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/631656.html
TechArticleThe original purpose of php to delete directories and files is to first check whether there are files in this directory. If there are, they are files. If it is a folder, call this function to delete it. If it is a file, call unlink directly...
Statement:
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