删除目录及目录下的文件

function deldir($dir) {
    if (false === is_dir($dir)) {
        return true;
    }
    //先删除目录下的文件:
    $dh = opendir($dir);
    while ($file = readdir($dh)) {
        if ($file != "." && $file != "..") {
            $fullpath = $dir . "/" . $file;
            if (!is_dir($fullpath)) {
                unlink($fullpath);
            } else {
                deldir($fullpath);
            }
        }
    }
    closedir($dh);
    //删除当前文件夹:
    if (rmdir($dir)) {
        return true;
    } else {
        return false;
    }
}
Last modification:September 20th, 2018 at 09:21 am

Leave a Comment