用PHP删除非空目录?

时间:2011-08-19 05:48:09

标签: php

  

可能重复:
  How do I recursively delete a directory and its entire contents (files+sub dirs) in PHP?

我需要删除PHP中的目录。没有内置的方法来删除包含文件的目录,所以我知道我必须使用其他人的脚本。

我发现了,我想我会用:

<?php

// simply pass the directory to be deleted to this function

function rrmdir($dir)
{
    if (is_dir($dir)) // ensures that we actually have a directory
    {
        $objects = scandir($dir); // gets all files and folders inside
        foreach ($objects as $object)
        {
            if ($object != '.' && $object != '..')
            {
                if (filetype($dir . '/' . $object) == 'dir')
                {
                    // if we find a directory, do a recursive call
                    rrmdir($dir . '/' . $object);
                }
                else
                {
                    // if we find a file, simply delete it
                    unlink($dir . '/' . $object);
                }
            }
        }
        // the original directory is now empty, so delete it
        rmdir($dir);
    }
}
?>

我看不出它有什么问题,我只是有点紧张,不仅要删除文件,还要整个目录,并递归地说。

有什么理由不行吗?你有没有使用过不同的东西?

0 个答案:

没有答案
相关问题