Using RecursiveIteratorIterator to view files in a folder is fine. Can I get the parent path and full path?

I need to specify all the files (with specific extensions) in the folder and its subfolders. I used RecursiveIteratorIterator, as described in @Matthew's remark in PHP, listing all the files in a directory .

  • I set the search root to ".." and all file names get the prefix "../". How can I get only the file name?
  • How can I get the parent folder name in addition to the file name?
  • ... and how can I get the full path to the file name?

And the last: displaying a tree-file or, possibly, all directories that do not have subdirectories, is an example of what could be done when working with files. How do you recommend transferring this data on the client side and what does the data structure look like?

+7
source share
3 answers

$filename in this answer is not really a string. This is an object of type SplFileInfo , which can be used as a string, but also offers much more detailed information:

+8
source

Using this as a basis:

 $iter = new RecursiveDirectoryIterator('../'); foreach (new RecursiveIteratorIterator($iter) as $fileName => $fileInfo) { $fullPath = (string) $fileInfo; } 

Each $fileInfo will return a SplFileInfo object. The path and file name are easily accessible in addition to many other methods.

 phpSplFileInfo Object ( [pathName:SplFileInfo:private] => ./t.php [fileName:SplFileInfo:private] => t.php ) 

I may be mistaken, but to access the name of the parent folder, if you do not write it through the tree, the simplest one will use dirname and add ../ to the path.

+3
source
 <?php $iterator = new FilesystemIterator(dirname(__FILE__), FilesystemIterator::CURRENT_AS_PATHNAME); foreach ($iterator as $fileinfo) { echo $iterator->current()->getPathname() . "\n"; } ?> 
+1
source

All Articles