remove duplicates RecursiveIteratorIterator
I was thinking of using Re开发者_如何学JAVAcursiveIteratorIterator to display the "top-directories", in my example layout and puffar, but I cannot get it to work. The structure is as follows:
| images |
=> layout
=>lab
=> puffar
and the result is
layout
layout
layout
layout/lab
layout/lab
puffar
puffar
$directory = 'images';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
while($it->valid()) {
if (!$it->isDot()) {
echo $it->getSubPath() . "\n";
}
$it->next();
}
another thing that I cannot get to work is if I change the directory path to
$directory = 'newsletter.site.se/images';
it is not displaying anything. It would be fantasic if someone could help me. thanks linda
Do you really need RecursiveDirectoryIterator
if you just want to display the top directories?
Instead you could use:
<?php
foreach (new DirectoryIterator('.') as $entry) {
if (!$entry->isDot() && $entry->isDir()) {
echo $entry, '<br />';
}
}
If you want all directories, try: (for example)
<?php
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'), RecursiveIteratorIterator::SELF_FIRST);
foreach ($it as $entry) {
if ($entry->isDir()) {
echo $entry, '<br />';
}
}
About your second question, have you checked that the path is correct and readable for php?
精彩评论