Page 1 of 1
search for .PHP files in the current directory and sub direc
Posted: Fri Jan 02, 2004 10:29 pm
by jpell7
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...
Posted: Fri Jan 02, 2004 11:26 pm
by m3mn0n
Posted: Fri Jan 02, 2004 11:58 pm
by lazy_yogi
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;
}
Posted: Sat Jan 03, 2004 9:38 am
by jpell7
Ok cool, now what if i wanted it to JUST search for .PHP files?
Posted: Sat Jan 03, 2004 9:55 am
by JAM
Try:
Code: Select all
// From
} else {
$files[] = $file;
}
// To
} else {
if (substr($file,-4) == ".php") { $files[] = $file; }
}
Posted: Sat Jan 03, 2004 10:20 am
by jpell7
Yeah I did a similar way just now too:
Code: Select all
} else {
$ext = strrchr($file, ".");
if ($ext == ".php") { $filesї] = $file; }
do you think that works almost equivelently?
Posted: Sat Jan 03, 2004 10:45 am
by lazy_yogi
jpell7 wrote:do you think that works almost equivelently?
No.
Files can have more than one full stop. For example, I have files which I call 'class.className.php'
I would go with what JAM posted. The code is exactly what you want and is correct.
Posted: Sat Jan 03, 2004 11:07 am
by jpell7
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:
Code: Select all
<?
$file="my.favorte.file.on.earth.php";
$ext = strrchr($file, ".");
echo "$ext";
?>
By doing that, you will see that it still takes .php

Posted: Sat Jan 03, 2004 5:41 pm
by JAM
Would work either way. If it feels comfortable, use that instead.
