note 76904 deleted from function.scandir by nlopess
| From: | nlopess@php.net | Date: | Wed, 08 Aug 2007 15:10:11 +0000 |
| Subject: | note 76904 deleted from function.scandir by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-129346@lists.php.net to get a copy of this message | ||
Note Submitter: clancyhood at gmail dot com
----
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