gpt4 book ai didi

PHP RecursiveDirectoryIterator返回但不遍历匹配 "x"的目录

转载 作者:搜寻专家 更新时间:2023-10-31 21:22:22 24 4
gpt4 key购买 nike

我正在用 PHP 编写磁盘目录应用程序。我的脚本遍历目录,将所有文件名和元数据存储在数据库中。有些目录我不想向下移动。我希望迭代器简单地返回这些目录的名称,就好像它们是文件一样,然后移动到下一个兄弟。我已经实现了一个 RecursiveCallbackFilterIterator,它允许根据匹配的文件名模式省略目录:

$filter = array(".app");

$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
$zpath,
RecursiveDirectoryIterator::SKIP_DOTS
),
function ($current, $key, $iterator) use ($filter) {
$match = 0;
foreach ($filter as $skip) {
if (substr($current->getBaseName(), -4, 4) == $skip) {
$match = 1;
}
}
if ($match) {
return false;
} else {
return true;
}
}
),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD
);

foreach ($files as $splFileInfo) {
$path = $splFileInfo->getRealPath();
echo $path."\n";
}

我的问题是,如何修改这段代码,使与模式匹配的目录包含在结果集中,但不返回给迭代器以供进一步遍历?

到目前为止,我发现的所有 RecursiveCallbackFilterIterator 示例都显示了上述内容的一些变化(例如,省略了某些文件或目录)。我只想返回与模式匹配的目录名称,然后转到下一个同级。

换句话说,我需要把这个:

File1.txt
File2.txt
Folder1/
Folder1/FileA.txt
Folder1/FileB.txt
MyThing.app/
MyThing.app/Contents/
Mything.app/Contents/Manifest.plist
Mything.app/Menu.nib
Portfolio.zip
Zee.txt

进入这个:

File1.txt
File2.txt
Folder1/
Folder1/FileA.txt
Folder1/FileB.txt
MyThing.app
Portfolio.zip
Zee.txt

最佳答案

我创建了一个 eval.in为了测试这个,虽然在这个环境中我无法创建目录,所以我只测试了文件,但也应该与目录一起工作。

file_put_contents("./file2.txt", "test");
file_put_contents("./Zee.txt", "test");
file_put_contents("./fileA.txt", "test");
file_put_contents("./fileB.txt", "test");
file_put_contents("./manifest.plist", "test");
file_put_contents("./manifest.app", "test");
file_put_contents("./MyApp.app", "test");
file_put_contents("./Menu.nib", "test");
$zpath=realpath("./");

$filter = array(".app");
$appFolders =array();


$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
$zpath,
RecursiveDirectoryIterator::SKIP_DOTS
),
function ($current, $key, $iterator) use ($filter) {
foreach ($filter as $skip) {
preg_match_all("(".$skip.")", $current->getRealPath(), $result);
if (!empty($result[0])) {
$GLOBALS["appFolders"][] =$current->getRealPath();
return false;
}
}
return true;
}
),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD
);

echo "\nFiles:\n";
foreach ($files as $splFileInfo) {
$path = $splFileInfo->getRealPath();
echo $path."\n";
}
echo "\nAppFolders:\n";
foreach ($appFolders as $app){
echo $app."\n";
}

输出是:

Files:
/tmp/execpad-4917ea112c86/file2.txt
/tmp/execpad-4917ea112c86/input-4917ea112c86
/tmp/execpad-4917ea112c86/manifest.plist
/tmp/execpad-4917ea112c86/Menu.nib
/tmp/execpad-4917ea112c86/output-4917ea112c86
/tmp/execpad-4917ea112c86/fileA.txt
/tmp/execpad-4917ea112c86/fileB.txt
/tmp/execpad-4917ea112c86/Zee.txt
/tmp/execpad-4917ea112c86/source-4917ea112c86

AppFolders:
/tmp/execpad-4917ea112c86/MyApp.app
/tmp/execpad-4917ea112c86/manifest.app

关于PHP RecursiveDirectoryIterator返回但不遍历匹配 "x"的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44025142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com