Is there a glitch with opendir / readdir?

Here's my PHP code:

<?php

// Enumerate the directories in styles
$styles_dir = 'styles/';

if($handle = opendir($styles_dir))
{
    while(FALSE !== ($file = readdir($handle)))
    {
        echo $file . '(' . is_dir($file) . ')<br>';
    }
}
?>

      

Here are the directories in styles

:

http://files.quickmediasolutions.com/php.jpg

And here's the output:

.(1)
..(1)
forest()
industrial()

      

Why not catalogs forest

and industrial

?

+2


a source to share


2 answers


The path to is_dir

is relative to the base file, so you really need to do the test like

is_dir($styles_dir . '/' . $file)

      



Note that this is masked for directories .

and ..

"as they exist all over the place.

+1


a source


You need a directory name prefix for the filename as it is_dir

works relative to the current directory.

Edit

echo $file . '(' . is_dir($file) . ')<br>';

      



to

echo $file . '(' . is_dir("$styles_dir/$file") . ')<br>';

      

Alternatively, you can change the directory to $styles_dir

using chdir and then your current code will work.

+1


a source







All Articles