Re: note 90626 deleted from function.scandir by thiago
| From: | Daniel Brown | Date: | Fri, 01 May 2009 12:00:40 +0000 |
| Subject: | Re: note 90626 deleted from function.scandir by thiago | ||
| References: | 1 2 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154071@lists.php.net to get a copy of this message | ||
A little silly, maybe, but useful. Re-added.
On Fri, May 1, 2009 at 03:43, <thiago@php.net> wrote:
> 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
> \*******************************/
>
> --
> PHP Notes Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
</Daniel P. Brown>
daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
50% Off All Shared Hosting Plans at PilotPig: Use Coupon DOW10000