我有一个文件夹,其中有一些文件和子文件夹。例如,我需要返回具有给定扩展名的文件名或相对地址(如果文件位于子文件夹中).html
.
例如,这是给定文件夹的结构:
- /文本/某事.ncx
- /文本/123.html
- 内容.opf
- toc.ncx
- Flow_0.html
- Flow_1.html
- Flow_2.html
所以代码应该返回这个(理想情况下在数组中):
- 文本/123.html
- Flow_0.html
- Flow_1.html
- Flow_2.html
它只是取出名字.html
扩展,但我真的不知道如何解决这个问题。
你可以使用RecursiveDirectoryIterator http://php.net/recursivedirectoryiterator to get all递归文件,然后使用过滤结果RegexIterator http://php.net/regexiterator仅迭代*.html
文件。为了得到相对地址, RecursiveDirectoryIterator::getSubPathname() http://php.net/recursivedirectoryiterator.getsubpathname可以使用。
$directory = '../path/to/your/folder';
$all_files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
$html_files = new RegexIterator($all_files, '/\.html$/');
foreach($html_files as $file) {
echo $html_files->getSubPathname(), PHP_EOL;
}
如果您确实需要数组(使用您通常不需要的可迭代对象),您可以轻松地在foreach
而不是echo
-ing 每个子路径。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)