Jump to content

[SOLVED] Scandir - parent directory scanning help


kevisazombie

Recommended Posts

 

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.

 

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.

 

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;
}


}

did you want it to keep falling back indefinitely until it finds something? (it would have to ignore the file's current dir if this is the case) or only in the parent dir?

 

If you just want to check the current dir, and then the parent dir... and then stop I would do something like

 

function buildNavList($startDir) {
$startDir = realpath($startDir);
$contents = scandir($startDir);

$result = array();
$fileBlacklist = array('.', '..', 'thumbs.db', basename(__FILE__));

foreach($contents as $file) {
	if(!in_array($file, $fileBlacklist)) {
		$result[] = $file;
	}
}

// Call the parent if there are no results
if(count($result) == 0) {
	$startDir = dirname($startDir);
	$result = buildNavList($startDir);
}

return $result;
}

print_r(buildNavList('.'));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.