PHP使用9行代码完成递归删除目录

利用了PHP目录递归迭代器能轻易使用9行代码完成递归删除目录操作,废话少说,直接上代码!

function rmdir_recursion($path) {
    $flag = FilesystemIterator::SKIP_DOTS | FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO;
    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, $flag),RecursiveIteratorIterator::CHILD_FIRST);
    foreach($objects as $value) {
        $realpath = $value->getRealpath();
        $value->isDir() ? rmdir($realpath) : unlink($realpath);
    }
    rmdir($path);
}
...