php 如何使用PHP删除包含内容的文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1334398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a folder with contents using PHP
提问by Fero
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()删除空文件夹,但不能删除有内容的文件夹。
回答by Alix Axel
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;
}
回答by Fenton
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!!!
小心点!!!
回答by Randell
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 得到它并且它有效。
回答by Krzysztof Krasoń
There is no single function build into PHP that would allow this, you have to write your own with rmdir and unlink.
PHP 中没有内置的单个函数允许这样做,您必须使用 rmdir 编写自己的函数并取消链接。
An example (taken from a comment on php.net docs):
一个例子(取自对php.net docs的评论):
<?
// 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 );
}
?>
回答by Ferdinand Beyer
You will have to delete all the files recursively. There are plenty example functions in the comments of the rmdirmanual page:
您必须递归删除所有文件。rmdir手册页的注释中有很多示例函数:
回答by ryeguy
You could always cheat and do
shell_exec("rm -rf /path/to/folder");
你总是可以作弊
shell_exec("rm -rf /path/to/folder");

