Why does isDot() fail on me? (PHP)
This is, because DirectoryIterator::current()
(the method, that is call within a foreach
-loop) returns an object, which is itself of type DirectoryIterator
. FileSystemIterator
(that RecursiveDirectoryIterator
extends) returns an object of SplFileInfo
be default. You can influence, what is return, via flags
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$pathToFolder,
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_SELF));
But in your case, you don't need to test, if an item is a dot-file. Just set FilesystemIterator::SKIP_DOTS
and they will not appear at all. Note, that this is also the default behavior.
The other answer is excellent, but for a different approach you can set the
SKIP_DOTS
flag:
<?php
$o_dir = new RecursiveDirectoryIterator($pathToFolder);
$o_dir->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$o_iter = new RecursiveIteratorIterator($o_dir);
foreach ($o_iter as $o_info) {
echo $o_info->getPathname(), "\n";
}
https://php.net/filesystemiterator.setflags