I have a folder in which I have some files and sub-folders. I need to return the names of the files or relative address (if the file is in sub-folder) that have a given extension, for example .html.
For example this is structure of given folder:
- /text/something.ncx
- /text/123.html
- content.opf
- toc.ncx
- Flow_0.html
- Flow_1.html
- Flow_2.html
So the code should return this (in an array ideally):
- text/123.html
- Flow_0.html
- Flow_1.html
- Flow_2.html
It’s just pulling out names with .html extension, but I really don’t how to solve this.
You could use a
RecursiveDirectoryIteratorto get all files recursively, then filter the result with aRegexIteratorto iterate over only the*.htmlfiles. To get the relative adress,RecursiveDirectoryIterator::getSubPathname()can be used.If you really need and array (with iterable objects you often don’t), you can easily build one up in the
foreachrather thanecho-ing each subpath.