如何使用PHP [duplicate]删除包含内容的文件夹

时间:2022-09-30 07:12:13

This question already has an answer here:

这个问题已经有了答案:

I need to delete a folder with contents using PHP. rmdir() and unlink() delete empty folders, but are not able to delete folders which have contents.

我需要用PHP删除一个包含内容的文件夹。rmdir()和unlink()删除空文件夹,但不能删除包含内容的文件夹。

6 个解决方案

#1


80  

This function will allow you to delete any folder (as long as it's writable) and it's files and subdirectories.

这个函数允许您删除任何文件夹(只要它是可写的)以及它的文件和子目录。

function Delete($path)
{
    if (is_dir($path) === true)
    {
        $files = array_diff(scandir($path), array('.', '..'));

        foreach ($files as $file)
        {
            Delete(realpath($path) . '/' . $file);
        }

        return rmdir($path);
    }

    else if (is_file($path) === true)
    {
        return unlink($path);
    }

    return false;
}

Or without recursion using RecursiveDirectoryIterator:

或者不使用RecursiveDirectoryIterator进行递归:

function Delete($path)
{
    if (is_dir($path) === true)
    {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST);

        foreach ($files as $file)
        {
            if (in_array($file->getBasename(), array('.', '..')) !== true)
            {
                if ($file->isDir() === true)
                {
                    rmdir($file->getPathName());
                }

                else if (($file->isFile() === true) || ($file->isLink() === true))
                {
                    unlink($file->getPathname());
                }
            }
        }

        return rmdir($path);
    }

    else if ((is_file($path) === true) || (is_link($path) === true))
    {
        return unlink($path);
    }

    return false;
}

#2


3  

You need to loop around the folder contents (including the contents of any subfolders) and remove them first.

您需要对文件夹内容(包括任何子文件夹的内容)进行循环,并首先删除它们。

There's an example here: http://lixlpixel.org/recursive_function/php/recursive_directory_delete/

这里有一个例子:http://lixlpixel.org/recursive_function/php/recursive_directory_delete/

Be careful with it!!!

小心! ! !

#3


3  

Here's a script that will do just what you need:

这里有一个脚本可以满足你的需要:

/**
 * Recursively delete a directory
 *
 * @param string $dir Directory name
 * @param boolean $deleteRootToo Delete specified top-level directory as well
 */
function unlinkRecursive($dir, $deleteRootToo)
{
    if(!$dh = @opendir($dir))
    {
        return;
    }
    while (false !== ($obj = readdir($dh)))
    {
        if($obj == '.' || $obj == '..')
        {
            continue;
        }

        if (!@unlink($dir . '/' . $obj))
        {
            unlinkRecursive($dir.'/'.$obj, true);
        }
    }

    closedir($dh);

    if ($deleteRootToo)
    {
        @rmdir($dir);
    }

    return;
}

I got it from php.net and it works.

我从php.net得到的,它起作用了。

#4


2  

There is no single function build into PHP that would allow this, you have to write your own with rmdir and unlink.

PHP中没有一个单独的函数允许这样做,您必须使用rmdir和unlink编写自己的函数。

An example (taken from a comment on php.net docs):

一个例子(摘自对php.net文档的评论):

<?
// ensure $dir ends with a slash
function delTree($dir) {
    $files = glob( $dir . '*', GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $file );
        else
            unlink( $file );
    }
    rmdir( $dir );
}
?>

#5


1  

You will have to delete all the files recursively. There are plenty example functions in the comments of the rmdir manual page:

您将不得不递归地删除所有文件。在rmdir手册页的注释中有很多示例函数:

http://www.php.net/rmdir

http://www.php.net/rmdir

#6


0  

You could always cheat and do shell_exec("rm -rf /path/to/folder");

您可以始终作弊并执行shell_exec(“rm -rf /path/to/folder”);

#1


80  

This function will allow you to delete any folder (as long as it's writable) and it's files and subdirectories.

这个函数允许您删除任何文件夹(只要它是可写的)以及它的文件和子目录。

function Delete($path)
{
    if (is_dir($path) === true)
    {
        $files = array_diff(scandir($path), array('.', '..'));

        foreach ($files as $file)
        {
            Delete(realpath($path) . '/' . $file);
        }

        return rmdir($path);
    }

    else if (is_file($path) === true)
    {
        return unlink($path);
    }

    return false;
}

Or without recursion using RecursiveDirectoryIterator:

或者不使用RecursiveDirectoryIterator进行递归:

function Delete($path)
{
    if (is_dir($path) === true)
    {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST);

        foreach ($files as $file)
        {
            if (in_array($file->getBasename(), array('.', '..')) !== true)
            {
                if ($file->isDir() === true)
                {
                    rmdir($file->getPathName());
                }

                else if (($file->isFile() === true) || ($file->isLink() === true))
                {
                    unlink($file->getPathname());
                }
            }
        }

        return rmdir($path);
    }

    else if ((is_file($path) === true) || (is_link($path) === true))
    {
        return unlink($path);
    }

    return false;
}

#2


3  

You need to loop around the folder contents (including the contents of any subfolders) and remove them first.

您需要对文件夹内容(包括任何子文件夹的内容)进行循环,并首先删除它们。

There's an example here: http://lixlpixel.org/recursive_function/php/recursive_directory_delete/

这里有一个例子:http://lixlpixel.org/recursive_function/php/recursive_directory_delete/

Be careful with it!!!

小心! ! !

#3


3  

Here's a script that will do just what you need:

这里有一个脚本可以满足你的需要:

/**
 * Recursively delete a directory
 *
 * @param string $dir Directory name
 * @param boolean $deleteRootToo Delete specified top-level directory as well
 */
function unlinkRecursive($dir, $deleteRootToo)
{
    if(!$dh = @opendir($dir))
    {
        return;
    }
    while (false !== ($obj = readdir($dh)))
    {
        if($obj == '.' || $obj == '..')
        {
            continue;
        }

        if (!@unlink($dir . '/' . $obj))
        {
            unlinkRecursive($dir.'/'.$obj, true);
        }
    }

    closedir($dh);

    if ($deleteRootToo)
    {
        @rmdir($dir);
    }

    return;
}

I got it from php.net and it works.

我从php.net得到的,它起作用了。

#4


2  

There is no single function build into PHP that would allow this, you have to write your own with rmdir and unlink.

PHP中没有一个单独的函数允许这样做,您必须使用rmdir和unlink编写自己的函数。

An example (taken from a comment on php.net docs):

一个例子(摘自对php.net文档的评论):

<?
// ensure $dir ends with a slash
function delTree($dir) {
    $files = glob( $dir . '*', GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $file );
        else
            unlink( $file );
    }
    rmdir( $dir );
}
?>

#5


1  

You will have to delete all the files recursively. There are plenty example functions in the comments of the rmdir manual page:

您将不得不递归地删除所有文件。在rmdir手册页的注释中有很多示例函数:

http://www.php.net/rmdir

http://www.php.net/rmdir

#6


0  

You could always cheat and do shell_exec("rm -rf /path/to/folder");

您可以始终作弊并执行shell_exec(“rm -rf /path/to/folder”);