[英]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.