|
-
Jul 29th, 2002, 01:56 AM
#1
Thread Starter
Fanatic Member
filesystem help
is there a way to get the names of all the folders in a directory ?
any help is appreciated on this topic
-
Jul 29th, 2002, 06:02 AM
#2
Frenzied Member
I don't know if there is something built into PHP, but this is what I use:
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;
}
?>
You can include/exclude files or directories and it returns them into an array.
-
Jul 29th, 2002, 07:58 AM
#3
Hyperactive Member
here is also another tidbit:
PHP 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
hope this helps
-
Jul 29th, 2002, 11:49 AM
#4
Stuck in the 80s
I've used the code Kagey posted, but this is how I modified it:
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);
}
}
Now you have all the directories and all the files in two different arrays in alphabetical order.
-
Jul 29th, 2002, 12:12 PM
#5
Hyperactive Member
-
Jul 30th, 2002, 07:02 PM
#6
Thread Starter
Fanatic Member
thanx guys, i'll give them all a try
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|