简体   繁体   English

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

[英]How to scan a directory and all subdirectorys for specific files (with .php extension)

I've got a folder structure like this: 我有一个这样的文件夹结构:

Examplefolder
|---Folder1
|       |--- file1.php
|---Folder2
|       |---Subfolder
|              |--- subfile1.php
|              |--- subfile2.html
|---Folder3
|---file2.php

As you can see examplefolder is the main folder. 如您所见, examplefolder是主文件夹。 It can contain normal php oder oder fileextensions. 它可以包含普通的php oder oder文件扩展名。 Inside the main folder there are also subfolders (like folder1 and folder2). 在主文件夹内还有子文件夹(如folder1和folder2)。 An inside the subfolders there can be more folders. 在子文件夹内可以有更多文件夹。

Now I want to scan the examplefolder for files with the extension .php which should get saved into an array. 现在,我想扫描examplefolder中扩展名为.php的文件,该文件应保存到数组中。 How will this work? 这将如何运作? I also tried a method witch shows all files, but I want only the files with the php extension. 我还尝试了一种显示所有文件的方法,但是我只想要带有php扩展名的文件。

My example looks like this: 我的示例如下所示:

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");
                    }
                }
            }
        }

This is possible with a simple if statement using the substr() function to fetch the last 4 characters of the file name. 这可以通过使用substr()函数的简单if语句来获取文件名的最后4个字符来实现。 Like so: 像这样:

$file = "test-file.php";

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

So if the last 4 characters are equal to .php, proceed otherwise do something else. 因此,如果最后4个字符等于.php,请继续执行其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅使用.htaccess禁用所有子目录的php - Disable php for all sub Subdirectorys only with .htaccess 如何扫描目录中的所有文件,然后加载所有文件 - How scan all the files in a directory and then load all the files 在特定时间后从目录和所有子目录中修改的PHP删除(.extension)文件 - PHP delete (.extension) files that are modified after specific time from directory and all sub-directories 如何删除目录中除PHP中特定文件夹外的所有文件夹和文件 - How to delete all folders and files in a directory except a specific folder in PHP 如何读取以PHP中的特定路径开头的目录中的所有文件 - How to read all files in a directory starting with specific path in PHP 如何在我所有的php文件中扫描自定义函数的所有用法? - how to scan all usages of a custom function in all my php files? PHP从目录中删除所有文件-排除文件扩展名 - PHP Remove all Files From a Directory - Exclude File Extension 仅为特定文件而不是特定扩展名的所有文件启用 PHP - Enable PHP for only a specific file instead of all files of a specific extension 递归扫描目录以查找具有特定扩展名的文件 - Scan directory for files with specific extensions recursively 如何拒绝访问目录中的所有文件,但允许访问其他目录中的特定php文件呢? - How can I deny access to all files in directory but allow access from specific php file in other directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM