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
array of existing files
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
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);
}
}
?>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;
}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
http://www.pgregg.com/projects/php/code/preg_find.phps
thanks again, all