使用include函数读取文件夹内的所有php文件

时间:2016-03-30 08:54:04

标签: php

我尝试读取文件夹中的所有文件。我创建了一个文件夹 sample ,在示例文件夹中我创建了php文件 testing.php 。此文件看起来像 sample / testing.php

我在示例文件夹之外创建另一个文件。我将其命名为 details.php

我将这些代码放在 details.php

function include_all_php($folder){

    foreach (glob("{$folder}/*.php") as $filename)
    {
        echo $filename;
        include($filename);
    }
}
$file =  __DIR__."/sample";
include_all_php($file);

showx();

在我的 testing.php 文件中我放了这些代码

function showx(){
    echo "heroo";
}

我收到此错误

Fatal error: Call to undefined function shox()

我回显$ filename。它显示我的文件testing.php,但为什么我不能调用函数showx()?我的代码有问题吗?

1 个答案:

答案 0 :(得分:1)

在此之前尝试:

function endsWith($haystack, $needle) {
    return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}

function include_all_php($folder){
$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder));
    foreach ($dir as $filename)
    {
        if(endsWith($filename, ".php")){
            include $filename;
        }
    }
}

include_all_php('/path/');