Requiring All Files in a Directory

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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Requiring All Files in a Directory

Post by tecktalkcm0391 »

Hello,

I have this code so far. I am trying to automatically scan a directory (and sub-directory) and include all files ending in ".actions.php" Right now it will include all of the ones in the main directory, but how can I get it to go into sub-directories?

Thanks!

Code: Select all

<?php
$path = APP_DIR.'actions/';
$dir = dir($path);
while(false !== ($filename = $dir->read())){ 
    if(substr($filename, -12) == '.actions.php') {
        print "$filename\n"; 
        require_once($path.$filename);
    }
}
$dir->close();
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Requiring All Files in a Directory

Post by requinix »

You have PHP 5?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Requiring All Files in a Directory

Post by tecktalkcm0391 »

Yes, I do have PHP 5.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Requiring All Files in a Directory

Post by jackpf »

I'd use glob() and is_file() or is_dir().

Then, if it's a directory, run the same function on it. Otherwise include it. It could be a bit slow though, depending on how many files you have.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Requiring All Files in a Directory

Post by tecktalkcm0391 »

There are quiet a few files, any other suggestions?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Requiring All Files in a Directory

Post by Eran »

use autoload. that is, if you are developing in OOP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Requiring All Files in a Directory

Post by requinix »

__autoload is definitely the best solution if you can use it that way. Thinking specifically about classes, like if the Foo_BarActions class is contained in foo/bar.actions.php: you can break the class name into "Foo" and "Bar" then look for the appropriate file.

Otherwise the SPL is has something for this:

Code: Select all

$dir = new RecursiveDirectoryIterator(APP_DIR . "actions/");
$iterator = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
 
foreach ($iterator as $file => $info) {
    if ($info->isFile() && substr($file, -12) == ".actions.php") include $file;
}
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Requiring All Files in a Directory

Post by tecktalkcm0391 »

This can work for something else, but I'm calling functions that are contained in the .action.php extention. :( But does autoload pretty much do as it say, and just loads a class only when you need it? Without having to call the function __autoload?
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Requiring All Files in a Directory

Post by Cirdan »

That's exactly what it does. Also take a look at spl_autoload_register
Post Reply