Re: Getting the filenames from a folder




Chung Leong wrote:
> I usually use glob() 'cause I'm lazy.
>
> http://fi.php.net/glob/

nice, i never saw that. I assume you could go *.*, can you give it
multiple matches, like *.jpg, *.gif ?

here is a func which can give an array of either folders or files:

$aListOfFiles = GetDirList($someDirectory, 'files');
$aListOfFfolders = GetDirList($someDirectory, 'folders');


function GetDirList($parent, $filesorfolders){
$oldDir = getcwd();
$list = array();

if ($handle = opendir($parent)) {
chdir($parent);
while (false !== ($file = readdir($handle))) {

if ($filesorfolders == "folders"){
if ( (is_dir($file)) && ($file != ".") && ($file !=
".."))
{$list[] = $file;
}}
if ($filesorfolders == "files"){
if ( (is_file($file)) && ($file != ".") && ($file !=
".."))
{$list[] = $file;
}}


}// while reading
closedir($handle);
chdir($oldDir);
}//if handle

sort($list);
reset($list);
return $list;
} //func

.



Relevant Pages

  • Re: A directory "grep" in RUBY?
    ... # the use passes in a string value representing a glob. ... # an array with all of the matching files, ... # iteration starts ... wanted the regex ...
    (comp.lang.ruby)
  • Re: Pattern Matching and skipping
    ... For each element of the array @files, create an alias for that element called $file, and execute the code in the block that follows. ... Open a file handle LOG whose filename is given by the expression ... used here in scalar context, so what open gets is the next element of the list in the glob... ...
    (comp.lang.perl.misc)
  • Re: I need help with my gallery
    ... I use the glob() function. ... It seems theres no way to strip the ... path from the globfunction so my array contains path and filename. ... Aah I see I was trying to use trimto remove the path from the array ...
    (comp.lang.php)
  • Re: a simple Q
    ... Senorita Farah wrote: ... > of a specific directory 'til it sees that a new file was ... Yes, use glob to get your directly list, but it into an array (use the ...
    (comp.lang.tcl)
  • Re: Perl 5.8.0 foreach & glob BUG
    ... `` Please check out the code below that shows that when I iterate of an array ... `` of values for use in a glob() function, then globreturns an empty string ... In all my years of Perl ... `` programming, I've never come across a single Perl bug, so even though this ...
    (comp.lang.perl.misc)