使用foreach循环包含文件夹中的文件 [英] Including files from folder with foreach loop

查看:152
本文介绍了使用foreach循环包含文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的简单代码来包含来自通用文件夹的所有文件。

  $ path = array(); 
$ ds = DIRECTORY_SEPARATOR;
$ path ['root'] = $ _ SERVER ['DOCUMENT_ROOT'];
$ path ['common'] = $ path ['root']。$ ds。common。$ ds;

//包含设置
需要$ path ['common']。$ ds。settings.php;

//包括常见的php文件
foreach(glob($ path ['common']。$ ds。*。php)as $ filename){
if $ filename!=settings.php)
需要$ path ['common']。$ ds。$ filename;

$ / code $ / pre

正如你所看到的,起初我使用

 需要$ path ['common']。$ ds。settings.php; 

然后用 foreach 循环。

我想知道,如果可以先包含 setting.php 文件,然后foreach循环中的所有其他文件,没有写上面的行?

解决方案

  $ files = glob($ path ['common' $ ds。*。php; 
array_unshift($ files,$ path ['common']。$ ds。settings.php);
foreach($ files as $ filename)
require_once $ filename;


I use following simple code to include all files from common folder.

$path=array();
$ds=DIRECTORY_SEPARATOR;
$path['root']=$_SERVER['DOCUMENT_ROOT'];
$path['common']=$path['root'].$ds."common".$ds;

//Include settings
require $path['common'].$ds."settings.php";

//including common php files
foreach (glob($path['common'].$ds."*.php") as $filename) {
    if($filename!="settings.php")
    require $path['common'].$ds.$filename;
}

As you see, at first I use

require $path['common'].$ds."settings.php";

then including all the rest of files with foreach loop.

I wonder, if it is possible to include setting.php file first then all other files inside foreach loop, without writing line above?

解决方案

$files=glob($path['common'].$ds."*.php";
array_unshift($files,$path['common'].$ds."settings.php");
foreach ($files as $filename)
  require_once $filename;

这篇关于使用foreach循环包含文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆