PDA

View Full Version : PHP Scandir - parent directory scanning help



kevisazombie
September 14th, 2008, 09:52 PM
Hi All,

I am trying to make a function that builds a navigation menu. The function scans the current directory ( ./) the page is in for any sub-directories and outputs links to them. If no sub-directories are found it falls back and scans the parent directory ( ../). The problem I am running into is using scandir on the parent never produces any results. It treats it as a legal directory because it dosen't throw an exception but it always returns a empty array.



function buildNavList(){
global $baseDIR;

$thisDIR = ".";
$listItems = Array();

while(count($listItems) == 0){
$dirContents = scandir($thisDIR);

for($i = 0; $i<count($dirContents); $i++){
if(is_dir($dirContents[$i]) and checkDirs($dirContents[$i]) == true ){
$listItems[] = $dirContents[$i];
}
}

if($thisDIR == "."){
echo"Thisdir = '$thisDIR'";
$thisDIR = $thisDIR."/";
}
$thisDIR = $thisDIR."../";


}



//print_r($listItems);
echo"Liste item count=".count($listItems)."\n\n";

for($i = 0; $i<count($listItems); $i++){

echo"\t\t\t\t<li><a href=\"".strtolower($listItems[$i])."/\">{$listItems[$i]}</a></li>\n";
}

}


function checkDirs($dirNameToCheck){// function checks to make sure the link being made is not leading to a directory we dont want listed
$dontLinkDirs = Array('.', '..', 'pics', 'thumbs', 'slideshow', 'text', 'about', 'contact', 'dont_touch', 'gallery', 'pricing');

if(!in_array($dirNameToCheck, $dontLinkDirs)){
return true;
}


}


I've tried scanning parent directorys using ( ../) and also complicated dirname(dirname(basename($thisfile) crap. Same results with both. The code i posted might be a little off i have been hacking at it but it should give you an idea. Id appreciate any help.

eirche
September 14th, 2008, 09:59 PM
If no sub-directories are found it falls back and scans the parent directory ( ../).

sounds complicated. try a recursive approach.