如何扫描目录和所有子目录中的特定文件(扩展名为.php)

如何扫描目录和所有子目录中的特定文件(扩展名为.php),php,parsing,directory,Php,Parsing,Directory,我有一个这样的文件夹结构: Examplefolder |---Folder1 | |--- file1.php |---Folder2 | |---Subfolder | |--- subfile1.php | |--- subfile2.html |---Folder3 |---file2.php if (file_exists($path) && is_dir($path)) {

我有一个这样的文件夹结构:

Examplefolder
|---Folder1
|       |--- file1.php
|---Folder2
|       |---Subfolder
|              |--- subfile1.php
|              |--- subfile2.html
|---Folder3
|---file2.php
if (file_exists($path) && is_dir($path))
        {
            $result = scandir($path);

            $files = array_diff($result, array('.', '..'));

            if (count($files) > 0)
            {


                foreach ($files as $file)
                {
                    if (is_file("$path/$file"))
                    {
                        $array[] = $file;
                    }
                    else if (is_dir("$path/$file"))
                    {
                        $array[] = $this->getComponentPathNames("$path/$file");
                    }
                }
            }
        }
如您所见,示例文件夹是主文件夹。它可以包含普通的php文件扩展。在主文件夹中还有子文件夹(如folder1和folder2)。a在子文件夹中可以有更多文件夹

现在我想扫描examplefolder,查找扩展名为.php的文件,这些文件应该保存到数组中。这将如何工作?我还尝试了一种显示所有文件的方法,但我只想要具有php扩展名的文件

我的示例如下所示:

Examplefolder
|---Folder1
|       |--- file1.php
|---Folder2
|       |---Subfolder
|              |--- subfile1.php
|              |--- subfile2.html
|---Folder3
|---file2.php
if (file_exists($path) && is_dir($path))
        {
            $result = scandir($path);

            $files = array_diff($result, array('.', '..'));

            if (count($files) > 0)
            {


                foreach ($files as $file)
                {
                    if (is_file("$path/$file"))
                    {
                        $array[] = $file;
                    }
                    else if (is_dir("$path/$file"))
                    {
                        $array[] = $this->getComponentPathNames("$path/$file");
                    }
                }
            }
        }

这可以通过使用substr()函数的简单if语句获取文件名的最后4个字符来实现。像这样:

$file = "test-file.php";

if ((substr($file, -4)) == ".php") {
    echo "This";
} else {
    echo "this one";
}

因此,如果最后4个字符等于.php,则继续执行其他操作。

通过使用substr()函数的简单if语句获取文件名的最后4个字符,这是可能的。像这样:

$file = "test-file.php";

if ((substr($file, -4)) == ".php") {
    echo "This";
} else {
    echo "this one";
}
因此,如果最后4个字符等于.php,请继续,否则请执行其他操作