note 98626 deleted from function.readdir by danbrown
| From: | danbrown@php.net | Date: | Sun, 27 Jun 2010 19:23:40 +0000 |
| Subject: | note 98626 deleted from function.readdir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170508@lists.php.net to get a copy of this message | ||
Note Submitter: aloziak at gmail dot com
----
Upgrade of a function which return recursively files and directories of specified directory.
Originaly script is from site http://snippets.dzone.com/posts/show/155%22.
This upgrade can return only files or only directories or both of them as well as you can choose
specified key of an array 1) default settings like number [0]=dir_x, [1]=dir_y, [2]=file_xy... or 2)
key has value such us value of the key â [dir_x]=dir_x, [dir_y]=dir_y, [file_xy]=file_xy....
function directoryToArray($path, $directory, $recursive, $method=false, $key=false) {
// method: files | dir | both
// array_key: false => default settings â numbers start from 0 | value => same value
like array value
$array_items = array();
if ($handle = opendir($path.$directory)) {
while (false !== ($file = readdir($handle))) {
if (strcmp($file, '.')==0 || strcmp($file, '..')==0 ||
eregi('^\..*$', $file) || eregi('Thumbs.db', $file)) continue;
if (is_dir($path.$directory."/".$file)) {
if($recursive) $array_items = array_merge($array_items,
directoryToArray($path.$directory."/", $file, $recursive, $method, $key));
$file = $directory."/".$file;
if ($method=='dir') {
if ($key=='value') $array_items[$file] = preg_replace("/\/\//si",
"/", $file);
else $array_items[] = preg_replace("/\/\//si", "/", $file);
}
} elseif ($method=='files' || !$method) {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
closedir($handle);
}
return $array_items;
}