Zen
Zen

Reputation: 7385

Including an array of files in PHP

what i'm trying to do is include all the php files in a given directory into the main php file, I'm loading them into an array using Glob()

like so:

// get all php files  
$files = glob('*.php');

But when I try to include($files) I'm getting an error saying it doesn't like arrays, should I use a foreach statement? or is there a better way of doing this?

Upvotes: 2

Views: 98

Answers (4)

hollsk
hollsk

Reputation: 3137

You'd use a foreach statement, so foreach $files as $file, include($file).

Having said that, I'm not so sure it's a good idea from a security point of view because you're just hoovering up every file in a directory and including it - if a malicious file gets onto your server then glob() leaves you with no way to evaluate whether or not it ought to be there before it's included and parsed.

Upvotes: 2

Pascal MARTIN
Pascal MARTIN

Reputation: 400992

include() doesn't accept an array of files -- it only accept one file path as a parameter.

This means you have to loop over that array of files, including one file at a time :

foreach ($files as $file) {
    include $file;
}

Upvotes: 0

Aaron W.
Aaron W.

Reputation: 9299

foreach (glob("*.php") as $filename) {
    // do stuff
}

Upvotes: 4

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53607

foreach
(I need to put at least 30 characters)

Upvotes: -1

Related Questions