Page 1 of 1
array of existing files
Posted: Tue Sep 14, 2004 5:40 pm
by bimo
Hi all,
Is there an array or function or some way that I can access all of the filenames that exist in a given location? I am trying to write a search function that can return multiple files (or their names at least).
I thought there might be something similar to the $_POST array except for files that exist in a given directory.
Thanks for your help,
Holly
Posted: Tue Sep 14, 2004 5:47 pm
by kettle_drum
Kind of. Take a look at this example from the manual:
Code: Select all
<?php
$dir = "/tmp/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
?>
Posted: Tue Sep 14, 2004 5:48 pm
by timvw
Code: Select all
function filesInDirectory($directory) {
$results = array();
if ($handler = @opendir($directory)) {
while ($file = readdir($handler)) {
if (!is_dir($directory.'/'.$file)) {
$results[] = $file;
}
}
closedir($handler);
}
return $results;
}
with a little imagination and recursion it does everything you want

Posted: Tue Sep 14, 2004 5:59 pm
by feyd
there's [php_man]glob[/php_man] too.. if you have a newer version of php.
Posted: Tue Sep 14, 2004 6:30 pm
by bimo
you guys are fantastic! All of your sugestions look great but unfortuanately I have to run to class and don't have time to check them out right now.
I'll post back if I run into any snags (which I'm sure I will.
h
Posted: Tue Sep 14, 2004 10:55 pm
by bimo
While at the glob() page I saw that someone created a function that is just what I am looking for: preg_find(), which matches files against a given pattern and puts them in an array.
http://www.pgregg.com/projects/php/code/preg_find.phps
thanks again, all