note 98626 added to function.readdir
| From: | aloziak at gmail dot com | Date: | Sun, 27 Jun 2010 19:17:27 +0000 |
| Subject: | note 98626 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170507@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 69.147.83.197
Probable Submitter: 90.176.201.25
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/98626
Del: integrated -- https://master.php.net/note/delete/98626/integrated
Del: useless -- https://master.php.net/note/delete/98626/useless
Del: bad code -- https://master.php.net/note/delete/98626/bad+code
Del: spam -- https://master.php.net/note/delete/98626/spam
Del: non-english -- https://master.php.net/note/delete/98626/non-english
Del: in docs -- https://master.php.net/note/delete/98626/in+docs
Del: other reasons-- https://master.php.net/note/delete/98626
Reject -- https://master.php.net/note/reject/98626
Search -- https://master.php.net/manage/user-notes.php