如何使用rmdir删除Windows目录?

时间:2012-12-11 09:48:11

标签: php windows directory-permissions

如何删除rmdir的目录?

目录为空但我无法删除/删除它。

$root_dir = "C:/xampp/htdocs/";    
$dir = $root_dir.'tmp/';    
rmdir($dir);

3 个答案:

答案 0 :(得分:1)

function removedir($dirname)
{
    if (is_dir($dirname))
    $dir_handle = opendir($dirname);
    if (!$dir_handle)
    return false;
    while($file = readdir($dir_handle)) {
        if ($file != "." && $file != "..") {
            if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
            else
            {
                $a=$dirname.'/'.$file;
                removedir($a);
            }
        }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}

答案 1 :(得分:1)

如果权限不是问题,那么tmp目录可能包含文件。 rmdir只删除空目录。使用此功能删除目录+所有文件:

function deleteDirectory($dir) {
    if (!file_exists($dir)) return true;
    if (!is_dir($dir)) return unlink($dir);
    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') continue;
        if (!deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false;
    }
    return rmdir($dir);
}

检查http://us3.php.net/manual/en/function.rmdir.php#91797以获取参考:)

答案 2 :(得分:1)

让我惊呆了一段时间的重要考虑因素:确保您没有func intOrThrow(arg: Int) throws -> AnyObject? { if arg < 0 { throw NSError(domain: "test", code: 400, userInfo: nil) } else if arg == 0 { return ["ZERO"] } else if arg > 1000 { return nil } return arg * 2 } func strOrNil(arg: Int) -> String? { if arg < 0 || arg > 1000 { return nil } return "NUMBER\(arg)" } print("before intOrThrow(100) and optional unwrap") if let x = try? self.intOrThrow(100) as? [String], // incorrect type results = x?.count { print("count is \(results). x is \(x)") } print("before intOrThrow(0) and optional unwrap") if let x = try? self.intOrThrow(0) as? [String], // good type results = x?.count { print("count is \(results). x is \(x)") } print("before intOrThrow(-100) and optional unwrap") if let x = try? self.intOrThrow(-100) as? [String], // throw results = x?.count { print("count is \(results). x is \(x)") } print("before intOrThrow(1111) and optional unwrap") if let x = try? self.intOrThrow(1111) as? [String], // nil results = x?.count { print("count is \(results). x is \(x)") } print("before intOrThrow(200) and block") if let x = try? self.intOrThrow(200) as? [String] { // incorrect type print("count is \(x?.count). x is \(x)") // still require ! or ?, else compile error } print("before intOrThrow(0) and block") if let x = try? self.intOrThrow(0) as? [String] { // good type print("count is \(x?.count). x is \(x)") // still require ! or ?, else compile error } print("before intOrThrow(-200) and block") if let x = try? self.intOrThrow(-200) as? [String] { // throw print("count is \(x!.count). x is \(x)") // still require ! or ?, else compile error } print("before intOrThrow(2222) and block") if let x = try? self.intOrThrow(2222) as? [String] { // nil print("count is \(x?.count). x is \(x)") // still require ! or ?, else compile error } print("done intOrThrow") print("before strOrNil(3333) and block") if let x = self.strOrNil(2222) { // nil, no type cast print("count is \(x.lowercaseString). x is \(x)") // don't require ! or ? } print("done strOrNil") 进入要删除的目录,如果有,{I}之前已经删除了chdir()。您无法删除Windows上的当前工作目录。