使用RecursiveDirectoryIterator :: SKIP_DOTS显示文件

时间:2018-10-24 18:35:46

标签: php

我希望得到这样的结果:

/path/to/files/test.png
/path/to/files/folder1/test.png
/path/to/files/folder2/test.png

相反,我得到了:

/path/to/files/.hidden_folder1/test.png
/path/to/files/test.png
/path/to/files/folder1/test.png
/path/to/files/folder2/test.png

我不希望出现带点的子目录:

我的代码如下:

$ufo = '/path/to/files/';
$Directory = new RecursiveDirectoryIterator($ufo,RecursiveDirectoryIterator::SKIP_DOTS);
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+(.png)$/i', RecursiveRegexIterator::GET_MATCH);

foreach($Regex as $name => $Regex){
    echo $name.'<br>';
}

:: SKIP_DOTS似乎不起作用。

我也尝试过:

$ufo = '/path/to/files/';
$Directory = new RecursiveDirectoryIterator($ufo);
$Iterator = new RecursiveIteratorIterator($Directory,RecursiveDirectoryIterator::SKIP_DOTS);
$Regex = new RegexIterator($Iterator, '/^.+(.png)$/i', RecursiveRegexIterator::GET_MATCH);

foreach($Regex as $name => $Regex){
    echo $name.'<br>';
}

上面的代码返回:

/path/to/files/folder1/test.png
/path/to/files/folder2/test.png

这会删除.test_folder子目录,但也会删除当前目录。

我尝试搜索无数示例,但无法找出正确的格式来递归返回文件(包括当前路径),而排除以开头的目录。

1 个答案:

答案 0 :(得分:0)

使用setFlags(RecursiveDirectoryIterator::SKIP_DOTS)方法。试试这个:

$ufo = '/path/to/files/';
$Directory = new RecursiveDirectoryIterator($ufo);
$Directory->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+(.png)$/i', RecursiveRegexIterator::GET_MATCH);

foreach($Regex as $name => $Regex){
    echo $name.'<br>';
}

类似的问题here