is there a way to get the names of all the folders in a directory ?
any help is appreciated on this topic :)
Printable View
is there a way to get the names of all the folders in a directory ?
any help is appreciated on this topic :)
I don't know if there is something built into PHP, but this is what I use:
You can include/exclude files or directories and it returns them into an array.PHP Code:<?php
//Returns the contents of a dir into an array
function dir_contents($directory, $files = true, $directories = true) {
//Read all the files from the directory
if ($dir = opendir($directory)) {
while (false !== ($file = readdir($dir))) {
//If we want this file, add it to the array
if ((is_file($file) && $files) || (is_dir($file) && $directories))
$return_value[] = $file;
}
closedir($dir);
}
//Return the files/dirs
return $return_value;
}
?>
here is also another tidbit:
hope this helpsPHP Code:if( !($dir = opendir('/images')) ) die('Cannot open folder');
while($file = readdir($dir)) {
if($file != '.' && $file != '..' && is_dir($file) ) {
echo $file . '<br>';
}
}
// note: they will not be in alphabetical order. you can add them
// to an array then sort that array instead of printing out the
// folder names
I've used the code Kagey posted, but this is how I modified it:
Now you have all the directories and all the files in two different arrays in alphabetical order.PHP Code:if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($file)) {
$dirs[] = $file;
} else {
$files[] = $file;
}
}
}
closedir($handle);
if (count($dirs) != 0) {
sort($dirs);
}
if (count($files) != 0) {
sort($files);
}
}
sweet deal
thanx guys, i'll give them all a try ;)