Why does isDot() fail on me? (PHP)

后端 未结 2 466
夕颜
夕颜 2021-02-13 03:25

I\'m finalizing a code segment that lists the files in a directory. I have no problems listing the files in a directory but for some reason I can get the isDot() method to work

2条回答
  •  佛祖请我去吃肉
    2021-02-13 03:52

    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.

提交回复
热议问题