note 76904 added to function.scandir
| From: | clancyhood at gmail dot com | Date: | Sat, 04 Aug 2007 20:44:46 +0000 |
| Subject: | note 76904 added to function.scandir | ||
| Groups: | php.notes php.notes | ||
| Request: | Send a blank email to php-notes+get-129177@lists.php.net to get a copy of this message | ||
For a couple of years now I've been using this function... it was one of the first I ever wrote
and pre-dates my use of mysql, so directories were my database back then! I wrote this to extend
the functionality of scandir a bit to give you results dependent on what you're scanning for.
listdir( string $dir [, int|str|arr $filter [, str|arr $skips ]] )
Arguments
$dir: path to directory
$filter: can be numeric (1 or 2) or a string or array of file extensions
1 = directories only
2 = files only
string / array = extension(s) of files that you want to know about, e.g., 'mp3' or
array('jpg', 'png', 'gif).
$skips = whole filename(s) of files you don't want to know about, e.g. 'thumbs.db' on
windows.
<?php
function listdir($dir, $filter=false, $skips=false){
$ignore = array('.', '..');
if(!is_array($skips)){
array_push($ignore, $skips);
}else{
$skips = array_merge($ignore, $skips);
}
$scan = scandir($dir); $list = array();
foreach($scan as $result){
if(!in_array($result, $ignore)){
if(!is_array($filter)){
if($filter == false){
array_push ($list, $result);
}else{
if($filter == 1){
if(is_dir("$dir/$result")){
array_push($list, $result);
}
}
if($filter == 2){
if(!is_dir("$dir/$result")){
array_push($list, $result);
}
}
if(!is_numeric($filter)){
$ext = array_pop(explode('.',$result));
if(strtolower($ext) == $filter){
array_push($list, $result);
}
}
}
}else{
$ext = array_pop(explode('.',$result));
if(in_array(strtolower($ext),$filter)){
array_push ($list, $result);
}
}
}
}
return $list ;
}
?>
Yeah, come to think of it I remember I was bored of filtering out '.' and '..' -
I think this was the first function I ever wrote but it's still a good one
----
Server IP: 69.147.83.197
Probable Submitter: 91.142.233.110
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/76904
Del: integrated -- https://master.php.net/note/delete/76904/integrated
Del: useless -- https://master.php.net/note/delete/76904/useless
Del: bad code -- https://master.php.net/note/delete/76904/bad+code
Del: spam -- https://master.php.net/note/delete/76904/spam
Del: non-english -- https://master.php.net/note/delete/76904/non-english
Del: in docs -- https://master.php.net/note/delete/76904/in+docs
Del: other reasons-- https://master.php.net/note/delete/76904
Reject -- https://master.php.net/note/reject/76904
Search -- https://master.php.net/manage/user-notes.php