search for .PHP files in the current directory and sub direc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jpell7
Forum Newbie
Posts: 6
Joined: Fri Jan 02, 2004 4:28 pm

search for .PHP files in the current directory and sub direc

Post 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...
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Here is a start: viewtopic.php?t=16029
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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;
}
jpell7
Forum Newbie
Posts: 6
Joined: Fri Jan 02, 2004 4:28 pm

Post by jpell7 »

Ok cool, now what if i wanted it to JUST search for .PHP files?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Try:

Code: Select all

// From
            } else {
                  $files[] = $file;
           } 
// To
            } else {
                  if (substr($file,-4) == ".php") { $files[] = $file; }
           }
jpell7
Forum Newbie
Posts: 6
Joined: Fri Jan 02, 2004 4:28 pm

Post by jpell7 »

Yeah I did a similar way just now too:

Code: Select all

&#125; else &#123; 
$ext = strrchr($file, ".");
                  if ($ext == ".php") &#123; $files&#1111;] = $file; &#125;
do you think that works almost equivelently?
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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.
jpell7
Forum Newbie
Posts: 6
Joined: Fri Jan 02, 2004 4:28 pm

Post 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 :D
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Would work either way. If it feels comfortable, use that instead. ;)
Post Reply