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

Viewed 6347

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 to make sure the file isn't a "." or ".." . The following below results in this error:

Fatal error: Call to undefined method SplFileInfo::isDot() in ....

Before I switched over to using the Recursive Iterator I was using the Directory Iterator and it worked fine. Is there anything wrong with the code below? It should work.

$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathToFolder));

//if there is a subdirectory it makes sure the proper extension is passed
foreach($files as $name => $file){      

            if (!$file->isDot()) {    //this is where it shuts me down          

            $realfile = str_replace($pathToFolder, "", $file);
            $url = getDownloadLink($folderID, $realfile);
        $fileArray[] = $url;            

        }       
}
2 Answers

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

Related