Re: Skipping files in a RecursiveDirectoryIterator loop

From: Date: Mon, 20 Nov 2017 02:11:01 +0000
Subject: Re: Skipping files in a RecursiveDirectoryIterator loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-326270@lists.php.net to get a copy of this message
Although I have many years of experience with other languages I don't have much experience with PHP. Based on what I know about programming in general, your general algorithm I say it is good. Also see Find all .php files in folder recursively - Stack Overflow <https://stackoverflow.com/questions/15054997/find-all-php-files-in-folder-recursively> for other possibilities. The following works for me for the current directory of the script. Evidently RecursiveDirectoryIterator can skip the dot directories simply by specifying the option. $di = new RecursiveDirectoryIterator(__DIR__,RecursiveDirectoryIterator::SKIP_DOTS); $it = new RecursiveIteratorIterator($di); foreach($it as $file) {
    echo $file, '<br>', PHP_EOL;
    }
Also, I would probably do it iteratively, not recursively, but I don't know any good reason to do that except I know how to do it, at least in C#.
Jennifer <mailto:jennifer@superiorshelving.com> Sunday, November 19, 2017 2:20 PM I want to iterate over every file in a particular directory, and all it's sub-directories. I'm using the code below and it works fine, but I'd like to know if I've done this properly, or if there's a better way to skip certain files and directories. Thanks, Jenni $rdi = new RecursiveDirectoryIterator($lists); foreach (new RecursiveIteratorIterator($rdi) as $file) { // To skip the . and .. directories if (substr($file->getFilename(), -1) == '.') { continue; } // To skip any files that begin with -- if (substr($file->getFilename(), 0, 2) == '--') { continue; } // To skip any directories that begin with -- if (substr($rdi, 0, 2) == '--') { continue; } // process files... }


« previous php.general (#326270) next »