[英]How to scan a directory and all subdirectorys for specific files (with .php extension)
我有一个这样的文件夹结构:
Examplefolder
|---Folder1
| |--- file1.php
|---Folder2
| |---Subfolder
| |--- subfile1.php
| |--- subfile2.html
|---Folder3
|---file2.php
如您所见, examplefolder是主文件夹。 它可以包含普通的php oder oder文件扩展名。 在主文件夹内还有子文件夹(如folder1和folder2)。 在子文件夹内可以有更多文件夹。
现在,我想扫描examplefolder中扩展名为.php的文件,该文件应保存到数组中。 这将如何运作? 我还尝试了一种显示所有文件的方法,但是我只想要带有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,请继续执行其他操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.