Page 1 of 1
Create array with file names from directory
Posted: Sun Nov 07, 2004 6:44 pm
by DroopyPawn
I'm sure this is a simple task but how do I create an array containing a list of file names in my directory (and optionally - subdirectories) ? I only want .html files in the list.
And then, how would I display a list of links to those pages (as html) using each file's META TITLE tag?
Posted: Sun Nov 07, 2004 7:04 pm
by BDKR
Have you checked
http://us2.php.net/manual/en/class.dir.php ? There is a good example of how to use the dir class. Once you've created an array with the dir class, iterate over it and check file names.
Cheers
Posted: Sun Nov 07, 2004 7:49 pm
by rehfeld
see if this works, i havent tested it,
Code: Select all
<?php
function recursive_scandir($dir)
{
$filenames = array();
if (!is_dir($dir) || !is_readable($dir)) {
return false;
}
if ($handle = opendir($dir)) {
while (false !== ($filename = readdir($handle))) {
if ($filename === '.' || $filename === '..') continue;
if (is_dir("$dir/$filename")) {
$nested_filenames = recursive_scandir("$dir/$filename");
if (is_array($nested_filenames)) {
$filenames = array_merge($filenames, $nested_filenames);
}
} else {
$filenames[] = "$dir/$filename";
}
}
closedir($handle);
}
return $filenames;
}
$dir = 'your_dir';
$all_files = recursive_scandir($dir);
print_r($all_files); // just so you can see it all
$html_files = array();
foreach ($all_files as $file) {
if ('html' === end(explode('.', $file))) {
$html_files[] = $file;
}
}
print_r($html_files);
?>