Jump to content

get no of folders help


cinek

Recommended Posts

I need to get the number of folders that are in the projects folder. I tried this but it's showing I only have 2 folders in that folder (there are 23 folders in there)

$dirs = 0;	$dir = "./projects";	$scan = scandir($dir);	foreach($scan as $result)	{		if(is_dir($result))		{			$dirs++;		}	}		echo "there are $dirs dirs";

any ideas what's wrong?edit: if I get rid of the if statement, i'm getting 25 dirs - but there are only 23 :S

// get amount of dirs	$dirs = 0;	$dir = "./projects";	$scan = scandir($dir);	foreach($scan as $result)	{			$dirs++;	}		echo "there are $dirs dirs";

Link to comment
Share on other sites

The two "extra" directories you are seeing are probably the current directory (./) and the parent directory (../). You can filter for those names (or just subtract 2).

Link to comment
Share on other sites

If you var_dump() the results from scandir(), you'll probably realize why is is_dir() failing.scandir() takes a path as an argument, and returns an array with the names (not paths!!!) of all items within the specified folder path, plus the "." and ".." names representing the current and parent folder.is_dir() takes a path, and checks if what's at that path is an existing folder.Armed with this knowledge, I think you'll agree with me that the following is what you need to do:

 $dirs = 0; $dir = "./projects"; $scan = scandir($dir); foreach($scan as $result) { if(is_dir($dir . '/' . $result)) { $dirs++; } }

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...