alc*_*ado 351
现在至少有两种选择.
在删除文件夹之前,删除它的所有文件和文件夹(这意味着递归!).这是一个例子:
public static function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
self::deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}
Run Code Online (Sandbox Code Playgroud)如果你使用的是5.2+,你可以使用RecursiveIterator来完成它而不需要自己进行递归:
$dir = 'samples' . DIRECTORY_SEPARATOR . 'sampledirtree';
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it,
RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dir);
Run Code Online (Sandbox Code Playgroud)use*_*886 184
我通常用它来删除文件夹中的所有文件:
array_map('unlink', glob("$dirname/*.*"));
Run Code Online (Sandbox Code Playgroud)
然后你就可以做到
rmdir($dirname);
Run Code Online (Sandbox Code Playgroud)
You*_*nse 79
删除包含所有文件的目录的最简单方法是什么?
system("rm -rf ".escapeshellarg($dir));
Run Code Online (Sandbox Code Playgroud)
Bla*_*ise 48
完成工作的短功能:
function deleteDir($path) {
return is_file($path) ?
@unlink($path) :
array_map(__FUNCTION__, glob($path.'/*')) == @rmdir($path);
}
Run Code Online (Sandbox Code Playgroud)
我在这样的Utils类中使用它:
class Utils {
public static function deleteDir($path) {
$class_func = array(__CLASS__, __FUNCTION__);
return is_file($path) ?
@unlink($path) :
array_map($class_func, glob($path.'/*')) == @rmdir($path);
}
}
Run Code Online (Sandbox Code Playgroud)
强大的功能带来了很大的责任:当您使用空值调用此函数时,它将删除以root(/)开头的文件.作为安全措施,您可以检查路径是否为空:
function deleteDir($path) {
if (empty($path)) {
return false;
}
return is_file($path) ?
@unlink($path) :
array_map(__FUNCTION__, glob($path.'/*')) == @rmdir($path);
}
Run Code Online (Sandbox Code Playgroud)
Pla*_*nox 18
这是一个较短的版本对我很有用
function deleteDirectory($dirPath) {
if (is_dir($dirPath)) {
$objects = scandir($dirPath);
foreach ($objects as $object) {
if ($object != "." && $object !="..") {
if (filetype($dirPath . DIRECTORY_SEPARATOR . $object) == "dir") {
deleteDirectory($dirPath . DIRECTORY_SEPARATOR . $object);
} else {
unlink($dirPath . DIRECTORY_SEPARATOR . $object);
}
}
}
reset($objects);
rmdir($dirPath);
}
}
Run Code Online (Sandbox Code Playgroud)
Ger*_*rre 16
正如在PHP手册页上的大多数投票评论中所见rmdir()(参见http://php.net/manual/es/function.rmdir.php),glob()函数不会返回隐藏文件. scandir()提供作为解决该问题的替代方案.
那里描述的算法(在我的例子中就像魅力一样)是:
<?php
function delTree($dir)
{
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
?>
Run Code Online (Sandbox Code Playgroud)
Gra*_*ble 13
// composer require symfony/filesystem
use Symfony\Component\Filesystem\Filesystem;
(new Filesystem)->remove($dir);
Run Code Online (Sandbox Code Playgroud)
但是我无法用这种方法删除一些复杂的目录结构,所以首先你应该尝试它以确保它正常工作.
我可以使用Windows特定的实现删除所述目录结构:
$dir = strtr($dir, '/', '\\');
// quotes are important, otherwise one could
// delete "foo" instead of "foo bar"
system('RMDIR /S /Q "'.$dir.'"');
Run Code Online (Sandbox Code Playgroud)
只是为了完整起见,这是我的旧代码:
function xrmdir($dir) {
$items = scandir($dir);
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$path = $dir.'/'.$item;
if (is_dir($path)) {
xrmdir($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
Run Code Online (Sandbox Code Playgroud)
这里有一个简单的递归来删除源目录中的所有文件,包括该目录:
function delete_dir($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
delete_dir($src . '/' . $file);
}
else {
unlink($src . '/' . $file);
}
}
}
closedir($dir);
rmdir($src);
}
Run Code Online (Sandbox Code Playgroud)
函数基于复制目录的递归.你可以在这里找到这个功能: 使用php将目录的全部内容复制到另一个目录
对我来说最好的解决方案
my_folder_delete("../path/folder");
Run Code Online (Sandbox Code Playgroud)
代码:
function my_folder_delete($path) {
if (in_array($path, ['.', '/'])) return; // ensure to avoid accidents
if(!empty($path) && is_dir($path) ){
$dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); //upper dirs are not included,otherwise DISASTER HAPPENS :)
$files = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $f) {if (is_file($f)) {unlink($f);} else {$empty_dirs[] = $f;} } if (!empty($empty_dirs)) {foreach ($empty_dirs as $eachDir) {rmdir($eachDir);}} rmdir($path);
}
}
Run Code Online (Sandbox Code Playgroud)
PS记住!
不要将空值传递给任何目录删除功能!!!(始终备份它们,否则有一天您可能会遇到灾难!)
您可以尝试如下:
/*
* Remove the directory and its content (all files and subdirectories).
* @param string $dir the directory name
*/
function rmrf($dir) {
foreach (glob($dir) as $file) {
if (is_dir($file)) {
rmrf("$file/*");
rmdir($file);
} else {
unlink($file);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个对我有用:
function removeDirectory($path) {
$files = glob($path . '/*');
foreach ($files as $file) {
is_dir($file) ? removeDirectory($file) : unlink($file);
}
rmdir($path);
return;
}
Run Code Online (Sandbox Code Playgroud)
我不敢相信有 30 多个答案。在 PHP 中递归删除文件夹可能需要几分钟时间,具体取决于目录的深度和其中的文件数量!你可以用一行代码来做到这一点......
shell_exec("rm -rf " . $dir);
如果您担心删除整个文件系统,请首先确保您的$dir路径正是您想要的。切勿允许用户输入可以直接删除文件的内容,而无需首先对输入进行大量验证。这是基本的编码实践。
小智 5
对 alcuadrado 的代码进行了一点修改 -glob看不到具有名称的文件,例如.htaccess我使用 scandir 并且脚本删除自身 - 检查__FILE__。
function deleteDir($dirPath) {
if (!is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = scandir($dirPath);
foreach ($files as $file) {
if ($file === '.' || $file === '..') continue;
if (is_dir($dirPath.$file)) {
deleteDir($dirPath.$file);
} else {
if ($dirPath.$file !== __FILE__) {
unlink($dirPath.$file);
}
}
}
rmdir($dirPath);
}
Run Code Online (Sandbox Code Playgroud)
Glob 函数不会返回隐藏文件,因此当尝试递归删除树时, scandir 可能更有用。
<?php
public static function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
316417 次 |
| 最近记录: |