...

View Full Version : is_dir() probLem....



fireblade
12-27-2007, 08:25 AM
Hi folks

I'm having a bit of trouble with is_dir() in PHP 4.1.2. I want to
create a menu dynamically from the names of folders in a web directory,
and I'm using this code to create an array of the folders:


function getDirContents($whichdir)
{

// directories to ignore
$exclusions[] = ".";
$exclusions[] = "..";
$exclusions[] = "includes";
$exclusions[] = "images";

$handle = opendir($whichdir);
while($dir = readdir($handle))
{
if ((is_dir($dir)) && (!(in_array($dir, $exclusions))))
{
$myDirList[] = $dir;
}
}
closedir($handle);
return $myDirList;

}


It works just fine if the function is called from a script where
$whichdir is the current working directory or a sub-directory in the
script's path, but not when $whichdir is 'up' a directory. I've tried
using relative and absolute paths.

The error is a peculiar one - the opendir() and readdir() seem to work,
but the array returned is empty. If I exclude the is_dir() check, I get
a full list of directories and files.

I've RTFM and can't find anything about is_dir() behaviour when
operating on a higher-level directory.

Anyone run into this one?

Thanks in advance

Inigoesdr
12-27-2007, 08:49 AM
You need to prepend the $whichdir directory to $dir when using is_dir() in this way.

function getDirContents($whichdir)
{
// directories to ignore
$exclusions[] = ".";
$exclusions[] = "..";
$exclusions[] = "includes";
$exclusions[] = "images";

$handle = opendir($whichdir);
while($dir = readdir($handle))
{
if ((is_dir($whichdir . $dir)) && (!(in_array($dir, $exclusions))))
{
$myDirList[] = $dir;
}
}
closedir($handle);
return $myDirList;
}
Make sure the directory you pass to the function has a trailing slash.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum