开发者

Including an array of files in PHP

开发者 https://www.devze.com 2023-02-20 15:13 出处:网络
what i\'mtrying 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()

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
(I need to put at least 30 characters)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号