Using RecursiveIteratorIterator to list files in folder is great. Can I get the parent path and full path as well?
file, php, spl
Solution
`$filename` in that answer is actually not a string. It is an object of type `SplFileInfo` which can be used like a string but it also offers much more detailed info:
- `SplFileInfo::getFilename()` -- filename.
- `SplFileInfo::getPathname()` -- path to the file
- `SplFileInfo::getPathInfo()` -- SplFileInfo object for the path
- `SplFileInfo::getRealPath()` -- canonicalized absolute pathname; see `realpath()` for more info and the difference to Pathname,
Problem
I need to list all files (which have certain extensions) in a folder and its sub-folders. I used RecursiveIteratorIterator as described in @Matthew 's great answer in PHP list all files in directory. - I'm setting the root of the search to ".." and all filenames get a prefix of "../". How can I get the filename only? - How can I get, in addition to the filename, its parent folder name? - ...and how can I get the full path of the filename? And one last thing: displaying a file-tree, or maybe all directories that have no sub-directories, are both examples of things that one might want to do when dealing with files. How would you recommend to pass this data to the client side and how would the data structure look like?