Re: Skipping files in a RecursiveDirectoryIterator loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxxxx>
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...
}


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux