【问题标题】:Remove only empty folders and subfolders in PHP仅删除 PHP 中的空文件夹和子文件夹
【发布时间】:2015-04-24 19:11:56
【问题描述】:

我有一个文件夹,里面有空文件夹和完整文件夹。

我想遍历所有这些,确定它们是否为 ,如果是,则删除它们。 我看到了一些适用的问题,但我无法弄清楚整体解决方案:

使用新的 PHP5 函数必须有一些简单可靠的方法来做到这一点?

类似(充满错误的伪代码)

<?php
$dir = new DirectoryIterator('/userfiles/images/models/');
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
         if(!(new \FilesystemIterator($fileinfo))->valid()) {
              rmdir($fileinfo);
         }
    }
}
?>

【问题讨论】:

  • 那么您想要DirectoryIterator 的解决方案还是glob 左右的解决方案?
  • 感谢您的回答。考虑到我在共享主机上,我想要最佳解决方案。那么也许要求最低的解决方案就可以了?

标签: php directory


【解决方案1】:

这应该适合你:

在这里,我只是使用glob()(PHP 4 >= 4.3.0,PHP 5)从特定路径获取所有目录。然后我遍历每个目录并检查它是否为空。

如果它是空的,我用rmdir() 删除它,否则我检查它是否有另一个目录并用新目录调用函数

<?php

    function removeEmptyDirs($path, $checkUpdated = false, $report = false) {
        $dirs = glob($path . "/*", GLOB_ONLYDIR);

        foreach($dirs as $dir) {
            $files = glob($dir . "/*");
            $innerDirs = glob($dir . "/*", GLOB_ONLYDIR);
            if(empty($files)) {
                if(!rmdir($dir))
                    echo "Err: " . $dir . "<br />";
               elseif($report)
                    echo $dir . " - removed!" . "<br />";
            } elseif(!empty($innerDirs)) {
                removeEmptyDirs($dir, $checkUpdated, $report);
                if($checkUpdated)
                    removeEmptyDirs($path, $checkUpdated, $report);
            }
        }

    }


?>

removeEmptyDirs

(PHP 4 >= 4.3.3, PHP 5)
removeEmptyDirs — Removes empty directory's

void removeEmptyDirs(string $path [, bool $checkUpdated = false [, bool $report = false ]])

说明

The removeEmptyDirs() function goes through a directory and removes every empty directory

参数

path
  The Path where it should remove empty directorys

checkUpdated
  If it is set to TRUE it goes through each directory again if one directory got removed

report
  If it is set to TRUE the function outputs which directory get's removed

返回值

None

举个例子:

如果$checkUpdatedTRUE 这样的结构将被完全删除:

- dir
   | - file.txt
   | - dir
        | - dir

结果:

- dir
   | - file.txt

如果是FALSE,默认情况下,结果将是:

- dir
   | - file.txt
   | - dir  //See here this is still here

如果$reportTRUE,你会得到这样的输出:

test/a - removed!
test/b - removed!
test/c - removed!

否则你没有输出

【讨论】:

  • 非常感谢您的广泛回答!当我们说话时,我正在对其进行测试,并将让您知道我的发现。这就是为什么我喜欢 SO!
  • @maartenmachiels 不客气。让我知道它是否适合你
  • 不幸的是,该脚本无法按照我的设想运行。通过我的日志,我注意到以下错误:mod_fcgid: stderr: PHP Fatal error: Can't use function return value in write context in /var/www/vhosts/xxx.xxx/subdomains/beta/httpdocs/tools/remove_empty.php on line 13 有关如何挽救此问题的任何线索?第 13 行是if(empty(glob($dir . "/*"))) {。我的 PHP 版本是 5.3.3。这可能是个问题吗?我使用的路径:/var/www/vhosts/xxx.xxx/subdomains/beta/httpdocs/userfiles/images/test2
  • 谢谢!我又试过了。一项测试成功,一项失败... 致命错误:/var/www/vhosts/xxx.xxx/subdomains/beta/httpdocs/tools/remove_empty.php on line 21 中允许的内存大小为 134217728 字节已用尽(尝试分配 523800 字节) 没有启用递归,它似乎运行得非常顺利。如果需要,我多次执行脚本没有问题。你怎么看? :-)
  • @maartenmachiels 好的,真的很奇怪,我测试了它,我既无法重现内存错误(尝试使用 600 个文件夹),也无法重现找不到目录的警告。 (确保您的路径正确或尝试使用:./your path