Home  >  Article  >  php教程  >  删除非空目录的php程序

删除非空目录的php程序

WBOY
WBOYOriginal
2016-06-13 10:00:55938browse

先检查此目录下是否有文件,如果有,是文件夹的话就再调用此函数删除,如果是文件就直接调用 unlink 删除,最后删除此目录。

 

 代码如下 复制代码

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

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