简体   繁体   中英

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?

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

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.

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;
}

foreach
(我至少要输入30个字符)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM