如何在当前目录中包含除文件本身之外的所有文件

时间:2011-11-06 00:39:40

标签: php function include

我编写了mini函数,用于将所有php文件包含在目录

foreach (glob("*.php") as $filename)
{
    include $filename;
}

如何在当前目录中包含除文件本身之外的所有文件(在哪个函数中)?

2 个答案:

答案 0 :(得分:1)

以下内容应该有所帮助。您应该只获取当前脚本文件名并与数组中的项进行比较:

$this_file = basename($_SERVER['SCRIPT_FILENAME']);
foreach (glob("*.php") as $filename)
{
    if ($filename !== $this_file) {
        include $filename;
    }
}

更多详情:

答案 1 :(得分:0)

foreach (glob("*.php") as $filename) {
  if (realpath($filename) !== __FILE__) include $filename;
}

或者

foreach (array_diff(array_map('realpath',glob("*.php")), array(__FILE__) as $filename) {
  include $filename;
}