Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

iterator

php

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;            

        }       
}
like image 645
thomascirca Avatar asked Jun 14 '11 00:06

thomascirca


1 Answers

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.

like image 60
KingCrunch Avatar answered Oct 02 '22 10:10

KingCrunch