Adrian M.
Adrian M.

Reputation: 7443

PHP include by name

I have a content folder which is full of other sub-directories named in the following way:

id1_1
id1_2
id1_3
id2_1
id2_2

And so on. Each of these folders contains a file template.php.

The number of folders is dynamic, so I need to find a way to import all the template.php from all folders starting with id_1_ into my index.php file. Is there any easy way to do this?

Upvotes: 0

Views: 92

Answers (2)

Mark
Mark

Reputation: 6254

You can use the glob() function:

$arrFiles = glob('./id_1_*/template.php');
foreach($arrFiles as $file) {
     include_once($file);
}

Upvotes: 3

Tim Green
Tim Green

Reputation: 2032

You can use a recursive function + foreach + glob:

http://www.php.net/glob

Upvotes: 1

Related Questions