note 90626 deleted from function.scandir by thiago
| From: | thiago@php.net | Date: | Fri, 01 May 2009 07:43:19 +0000 |
| Subject: | note 90626 deleted from function.scandir by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154069@lists.php.net to get a copy of this message | ||
Note Submitter: phpdotnet at lavavortex dot com
----
How a ninja may retrieve a list of files, files filtered by extension, or directories:
//NNNIIIinnnjaaa::
//array of files without directories... optionally filtered by extension
function file_list($d,$x){
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){
foreach(array_diff(scandir($d),array('.','..')) as
$f)if(is_dir($d.'/'.$f))$l[]=$f;
return $l;
}
/********************************************************************\
PRETTY PRETTY LIGHTS (DOCUMENTATION)
\********************************************************************/
/********************************************************************\
Overloaded PHP file listing function:
array file_list ( string $directory [, string $file_extension] )
$directory
path without backslash, e.g. "/home/public_html/blarg"
$file_extention
optionally filter specific filename extentions, e.g. ".jpg"
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TRANSLATION
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<<<
file_list("/home"); //return list of files (no directories)
file_list("/home", ".jpg"); //return only .jpg file list
\********************************************************************/
//(note: one slash (/) below and you enable all your test functions, guess where ;-))
/********************************************************************\
// TEST FUNCTIONS... IF THESE WORK, THIS FUNCTION WORKS ON THIS PLATFORM
echo "<hr><b>File List:</b><br>";
$n = file_list(getcwd());
if($n) foreach($n as $f) echo "$f<br>"; //current files
echo "<hr><b>Files with extension .php:</b><br>";
$n = file_list(getcwd(),".php");
if($n) foreach($n as $f) echo "$f<br>"; //files with .php extensions
echo "<hr><b>Directories:</b><br>";
$d = dir_list(getcwd());
if($d) foreach($d as $f) echo "$f<br>"; //directories
/********************************************************************/
/************\
RUNTIME NOTES:
file_list($arg1); // php issues a warning that there is no second parameter, but we know that, izz
ok
\************/
/*******************************\
TESTED AND WORKING ON 2009.04.30:
OS: Linux 2.6.9-78.0.17.ELsmp
APACHE: 2.2.9
PHP: 5.2.5
\*******************************/