[英]PHP delete (.extension) files that are modified after specific time from directory and all sub-directories
[英]Symfony Finder: Get all the files with a specific extension and all the directories within a specific directory
我正在使用Symfony Finder
获取具有特定扩展名的所有文件以及特定目录中的所有目录。
protected function getDirectoryContent(string $directory): array
{
$finder = Finder::create()
->in($directory)
->depth(0)
->name(['*.json', '*.php'])
->sortByName();
return iterator_to_array($finder, true);
}
这样,该方法只返回某个目录下所有扩展名为.php
或.json
的文件。 例如,我正在查找的目录结构如下:
/my/directory/
├── A
├── A.JSON
├── anotherfile.kas
├── file0.ds
├── file1.json
├── file2.php
├── file3.php
├── B
└── C
A
、 B
和C
是目录。
当我在上面显示的方法中将上述directory path
作为$directory
参数传递时,我得到一个包含以下元素的数组:
file1.json
file2.php
file3.php
太棒了!,但我的问题是,我怎样才能将所有directories
添加到结果数组中? 我的期望是得到一个如下所示的数组:
A
B
C
file1.json
file2.php
file3.php
在你的情况下,你和finder说话:
结果是错误的,因为这两个规则相互矛盾 - 因为您只想搜索文件。
但是,symfony finder 可以将CallbackIterator
与过滤器模型一起使用。 在这种情况下,您可以添加许多规则或条件。 在你的例子中:
namespace Acme;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
include __DIR__.'/vendor/autoload.php';
$finder = Finder::create();
$finder
->in(__DIR__)
->depth(0)
->filter(static function (SplFileInfo $file) {
return $file->isDir() || \preg_match('/\.(php|json)$/', $file->getPathname());
});
print_r(\iterator_to_array($finder));
在这种情况下,你说:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.