PHP空文件夹不能用rmdir命令删除

Oti*_*ght 0 php git directory unlink rmdir

我的代码如下:

<?php
    header("Location: ../");
    unlink("index.php");
    unlink("style.css");
    unlink("success.php");
    unlink("fail.php");
    unlink("remove.php");
    unlink("README.md");
    unlink(".gitignore");
    unlink(".git");
    rmdir("../Humble-Installer");
    die();
Run Code Online (Sandbox Code Playgroud)

但每次运行它我都会收到以下错误:

[17-Nov-2014 19:47:37 Pacific/Auckland] PHP Warning:  unlink(.git): Operation not permitted in /Users/user/Humble/admin/Humble-Installer/remove.php on line 10
[17-Nov-2014 19:47:37 Pacific/Auckland] PHP Warning:  rmdir(../Humble-Installer): Directory not empty in /Users/user/Humble/admin/Humble-Installer/remove.php on line 11
Run Code Online (Sandbox Code Playgroud)

我不知道,该目录是空的但不会删除...即使我删除unlink(."git");它仍然会抛出错误?

干杯.

kri*_*lfa 5

您可以使用此简单函数递归删除文件夹:

function rrmdir($dir) { 
    if (is_dir($dir)) { 
        $objects = scandir($dir); 
        foreach ($objects as $object) { 
            if ($object != "." && $object != "..") { 
                if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); 
            } 
        }
        reset($objects); 
        rmdir($dir); 
    }
}
Run Code Online (Sandbox Code Playgroud)

笔记:

unlink是一个文件,.git是一个目录,所以它不会删除,使用rmdir.如果你想以递归方式进行,请使用我上面写的函数.

更新

如果要使用RecursiveIteratorIterator,可以使用此功能:

/**
 * Remove directory recursively.
 *
 * @param string $dirPath Directory you want to remove.
 */
function recursive_rmdir($dirPath)
{
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
        $pathName = $path->getPathname();

        echo $pathName."\n";

        ($path->isDir() and ($path->isLink() === false)) ? rmdir($pathName) : unlink($pathName);
    }
}
Run Code Online (Sandbox Code Playgroud)