Reputation: 29
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. It can contain normal php oder oder fileextensions. Inside the main folder there are also subfolders (like folder1 and 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. How will this work? I also tried a method witch shows all files, but I want only the files with the php extension.
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");
}
}
}
}
Upvotes: 1
Views: 45
Reputation: 91
This is possible with a simple if statement using the substr() function to fetch the last 4 characters of the file name. 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.
Upvotes: 0