Reputation: 6081
I want to include
every file from every directory in my project. What I currently have is, that I include
every file from a specific directory
foreach (glob("*.php") as $filename)
{
include_once $filename;
}
But I also want to do the same for every directory I have and all the files in there. I heard of the __autoload
function, but I also need it sometimes in for non-class-functions.
Upvotes: 0
Views: 185
Reputation: 2541
Try this. This works for me.
$accepted_extension = array("jpeg", "jpg", "gif", "bmp", "png");
$return = array();
$results = scandir($folder);
foreach ($results as $result) {
if ($result == '.' or $result == '..')
continue;
else if (is_dir($folder . '/' . $result))
continue;
else {
$extn = substr($result, strpos($result, ".") + 1);
if (in_array($extn, $accepted_extension)) {
$return[] = $result;
}
}
}
Upvotes: 0
Reputation: 12002
On this man page, the first comment gives a function that lists all the files recursively. Just adapt it to match your needs:
<?php
function include_all_php_files($dir)
{
$root = scandir($dir);
foreach($root as $value)
{
if($value === '.' || $value === '..') {continue;}
if(is_file("$dir/$value") && preg_match('#\.php$#', $value))
{
include_once ("$dir/$value");
continue;
}
include_all_php_files("$dir/$value");
}
}
?>
Upvotes: 1
Reputation: 10050
Recursion is your friend.
/**
* @param string path to the root directory
*/
function include_files($dir) {
foreach (glob($dir . "/*") as $file) {
if(is_dir($file)){
include_files($file);
} else {
include_once($file);
}
}
}
Upvotes: 1