Home >类库下载 >PHP类库 >php delete all files and directories under directory

php delete all files and directories under directory

高洛峰
高洛峰Original
2016-10-29 10:40:041170browse

<?php
/**
 * 递归实现删除目录下的所有的文件和文件夹
 * @param $dir 要删除的目录
 * @param bool $deleteRootToo 是否删除根目录 默认不删除
 http://www.manongjc.com/article/1333.html
 */
function unlinkRecursive($dir, $deleteRootToo = false)
{
    if(!$dh = @opendir($dir))
    {
        return;
    }
    while (false !== ($obj = readdir($dh)))
    {
        if($obj == &#39;.&#39; || $obj == &#39;..&#39;)
        {
            continue;
        }
        if (!@unlink($dir . &#39;/&#39; . $obj))//删除文件, 如果是目录则返回false
        {
            unlinkRecursive($dir.&#39;/&#39;.$obj, true);
        }
    }
    // http://www.manongjc.com/article/1334.html
    closedir($dh);
    if ($deleteRootToo)
    {
        @rmdir($dir);//删除目录
    }
    return;
}
unlinkRecursive(&#39;dir&#39;);
?>


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
Previous article:PHP array sortNext article:PHP array sort

Related articles

See more