note 90626 added to function.scandir

From: Date: Fri, 01 May 2009 04:29:40 +0000
Subject: note 90626 added to function.scandir
Groups: php.notes 
Request: Send a blank email to php-notes+get-154067@lists.php.net to get a copy of this message
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 \*******************************/ ---- Server IP: 208.69.120.35 Probable Submitter: 70.167.114.178 ---- Manual Page -- http://www.php.net/manual/en/function.scandir.php Edit -- https://master.php.net/note/edit/90626 Del: integrated -- https://master.php.net/note/delete/90626/integrated Del: useless -- https://master.php.net/note/delete/90626/useless Del: bad code -- https://master.php.net/note/delete/90626/bad+code Del: spam -- https://master.php.net/note/delete/90626/spam Del: non-english -- https://master.php.net/note/delete/90626/non-english Del: in docs -- https://master.php.net/note/delete/90626/in+docs Del: other reasons-- https://master.php.net/note/delete/90626 Reject -- https://master.php.net/note/reject/90626 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154067) next »