如何使用PHP包含多个目录中的特定文件名

时间:2009-12-05 02:21:31

标签: php include filenames

我正在尝试在文件夹结构中包含特定的“管理视图”文件。我熟悉include / require或如何递归地包含一个文件夹,但我需要做的就是在每个目录中包含一个文件名 - admin_view.php文件。

所以,我的主意。结构看起来像这样:

要包含的文件位于root。

 - mods
  - type
    -- type1
       -admin_view.php
       -other files I do not need included
    -- type2
       -admin_view.php
       -other files I do not need included
    -- type3
       -admin_view.php
       -other files I do not need included

正如你所看到的,递归地包含文件不会工作,并且手动包含它们工作正常,直到我们开始从主“类型”文件夹中添加和删除目录..然后,我们必须手动编辑所有包含代码..所以,我希望找到一个函数或代码片段,它允许我查看带有'type'目录的目录,并且只包含一个特定的文件名(admin_view.php)

这是我到目前为止所拼凑的内容,但它不起作用...... :(

$modulesDir = array (
    ROOT_DIR.'mods/type',

);
$view_name = "admin_view.php";

function __autoload($view_name) {
    global $modulesDir;
    foreach ($modulesDir as $directory) {
        if (file_exists($directory . $view_name)) {
                require_once ($directory . $view_name);
                return;
        }
    }
}

非常感谢!这个地方是一个很棒的资源..我非常感谢它为开发社区提供的所有知识,纠正,澄清等等! Ĵ

修改

我能够获得加载了这个视图文件..但我觉得有更好的方法可以做到这一点:

澄清

我误用__autoload函数..因为我没有加载类..只是php文件包含在主文档中...抱歉错误,我正在学习:)

3 个答案:

答案 0 :(得分:1)

不确定这是否是代码中唯一的问题,但请查看:

$modulesDir = array (
    ROOT_DIR.'mods/type',
);

if (file_exists($directory . $view_name)) { ... }

您未使用$modulesDir

结束/中的文件夹名称

执行file_exists($directory . $view_name)时,实际上是在检查名为

的文件
ROOT_DIR.'mods/typeadmin_view.php'

而不是你想要的

ROOT_DIR.'mods/type/admin_view.php'

编辑:

关于__autoload($name)的使用,要记住的一件事是:

来自PHP Documentation: Autoloading Classes

  

您可以定义__autoload函数   这是自动调用以防万一   你正试图用一个   没有的类/接口   已定义。

这意味着如果您没有使用类或者您正在使用的类已经被“包含”,则不会被调用。

从您的问题来看,乍一看是否使用课程并不清楚,即使您使用__autoload()看起来不合适也是如此。

使用glob("/path/*/file.php")的方法似乎很好 它简单明了,完成工作 除非你发现它的具体问题,否则你应该好好去。

答案 1 :(得分:1)

您可以使用SPL的RecursiveDirectoryIterator来迭代目录及其子目录中的所有文件。
然后,您可以使用扩展FilterIterator的类将结果限制为特定文件。

e.g。

<?php
class EndsWithFilterIterator extends FilterIterator {
  protected $s;
  public function __construct($s, $it) {
    parent::__construct($it);
    $this->s = $s;
  }

  public function accept() {
    $c = parent::current();
    return false!==stripos($c, $this->s, strlen($c)-strlen($this->s));
  }
}


$directory = 'type/';
$it = new EndsWithFilterIterator('admin_view.php',
  new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory))
);
foreach($it as $f) {
  echo $f, "\n";
  // require_once $f;
}

答案 2 :(得分:0)

__autoload用于自动加载类包含。

这意味着每次创建PHP的新实例时,PHP都不知道该定义,PHP将调用__autoload函数并希望包含类定义。

我没有在代码中看到任何代码创建对象,如果您查看__autoload的PHP,他们会提供此示例。

<?php
function __autoload($class_name) {
    require_once $class_name . '.php';
}

$obj  = new MyClass1();
$obj2 = new MyClass2(); 
?>

如果您在创建控制器对象时想要包含视图,那么这样的控制器就必须像foobarController一样命名,这样才能正常工作。

$modulesDir = array (
    ROOT_DIR.'mods/type',

);
$view_name = "admin_view.php";

function __autoload($class_name) {
    global $modulesDir;
    if(preg_match('/(\w+)Controller/', $class_name, $match)){
      $class = $match[1];
      // TODO: include the class definition
      // require CONTROLLER_DIR . $class_name . '.php';
      foreach ($modulesDir as $directory) {
        if (file_exists($directory . strtolower($class) . '_view.php')) {
                require_once ($directory . strtolower($class) . '_view.php');
                return;
        }
      }
    }
}

$adminController = new AdminController();
相关问题