search for .PHP files in the current directory and sub direc
Moderator: General Moderators
search for .PHP files in the current directory and sub direc
Just like the title said, I am looking to search for .PHP files in the current directory and sub directories that follow.
Somewhat like a search spider, how can I do this?
I also want to echo the results out on the page...
Somewhat like a search spider, how can I do this?
I also want to echo the results out on the page...
Here is a start: viewtopic.php?t=16029
Code: Select all
// $files = getAllFiles(".");
// while (list ($key, $val) = each ($files))
// echo "$val<BR>\n";
function getAllFiles ($dir) {
$files = array();
$d = opendir ("$dir");
//list the files in the dir
while ($file = readdir ($d)) {
if ($file == ".." || $file == ".") continue;
if (is_dir ($dir."/".$file)) {
$dirFiles = getAllFiles($dir."/".$file);
foreach ($dirFiles as $f)
$files[] = $file."/".$f;
} else {
$files[] = $file;
}
}
closedir ($d);
sort($files);
return $files;
}Try:
Code: Select all
// From
} else {
$files[] = $file;
}
// To
} else {
if (substr($file,-4) == ".php") { $files[] = $file; }
}Yeah I did a similar way just now too:
do you think that works almost equivelently?
Code: Select all
} else {
$ext = strrchr($file, ".");
if ($ext == ".php") { $filesї] = $file; }Yes, I know but I am using strrchr, which takes the last occurance. So, no matter how many periods i have, it will just take the last one.
Here, make a quick php file and copy and paste this code into it and then run it:
By doing that, you will see that it still takes .php 
Here, make a quick php file and copy and paste this code into it and then run it:
Code: Select all
<?
$file="my.favorte.file.on.earth.php";
$ext = strrchr($file, ".");
echo "$ext";
?>