note 91218 deleted from function.scandir by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Jun 2009 13:09:50 +0000 |
| Subject: | note 91218 deleted from function.scandir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156259@lists.php.net to get a copy of this message | ||
Note Submitter: pocketjpaul at gmail dot com
----
Just a fix to the ninja's functions:
With this fix there is no notice when $x isn't set, and the function returns an empty array
instead of 'null' if there is no files/dirs: this is usefull when you use foreach on the
result.
<?php
//NNNIIIinnnjaaa::
//array of files without directories... optionally filtered by extension
function file_list($d,$x = null){
$l = array();
foreach(array_diff(scandir($d),array('.','..')) as
$f)if(is_file($d.'/'.$f)&&(($x)?ereg($x.'$',$f):1))$l[]=$f;
return $l;
}
//NNNIIIinnnjaaa::
//array of directories
function dir_list($d){
$l = array();
foreach(array_diff(scandir($d),array('.','..')) as
$f)if(is_dir($d.'/'.$f))$l[]=$f;
return $l;
}