note 40745 added to function.readdir
| From: | josh at duovibe dot com | Date: | Fri, 12 Mar 2004 20:31:29 +0000 |
| Subject: | note 40745 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66751@lists.php.net to get a copy of this message | ||
After looking for awhile for a function which retrieves a folder listing based on certain criteria
(return folder, files, or both; search recursively or not), I decided to write my own. Maybe
somebody out there will find it helpful.
Currently, it returns a flat list, but that can changed to return a nested list by editing the line:
$file_list = array_merge($file_list, directory_list($path ...
to
$file_list[] = directory_list($path ...
function directory_list($path, $types="directories", $recursive=0) {
// get a list of just the dirs and turn the list into an array
// @path starting path from which to search
// @types can be "directories", "files", or "all"
// @recursive determines whether subdirectories are searched
if ($dir = opendir($path)) {
$file_list = array();
while (false !== ($file = readdir($dir))){
// ignore the . and .. file references
if ($file != '.' && $file != '..') {
// check whether this is a file or directory and whether we're interested in that type
if ((is_dir($path."/".$file)) && (($types=="directories") ||
($types=="all"))) {
$file_list[] = $file;
// if we're in a directory and we want recursive behavior, call this function again
if ($recursive) {
$file_list = array_merge($file_list, directory_list($path."/".$file."/",
$types, $recursive));
}
} else if (($types == "files") || ($types == "all")) {
$file_list[] = $file;
}
}
}
closedir($dir);
return $file_list;
} else {
return false;
}
}
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40745
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40745&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40745&report=yes
Search -- http://master.php.net/manage/user-notes.php