Is there a special function that will get all files from a given directory and put them in an array?
| SitePoint Sponsor |
Is there a special function that will get all files from a given directory and put them in an array?





There is no special built in function but here is user-defined function.
PHP Code:<?
function list_files($dir = ".") {
$d = dir($dir);
while($file = $d->read()) {
if (($file != ".") && ($file != "..")) {
$files[] = $file;
}
else {
continue;
}
}
$d->close();
return $files;
}
//Sample Usage takes optional argument
//- path to directory to list, defaults to current dir.
$filelist = list_files("path/to/list");
foreach($filelist as $val) {
print "$val<br>";
}
exit;
?>
Please don't PM me with questions.
Use the forums, that is what they are here for.





You can do this:
Then you can split $dircontents by "\n" and you will get an array.PHP Code:passthru("ls DIRECTORY_NAME", $dircontents);
That should work...tell me if it doesn't.
Last edited by qslack; May 12, 2001 at 17:46.





Err, freddydoesphp got to it before me. And he came up with a better solution![]()
Bookmarks