getSettings() seems to only read and output 1 settings.php file in the directory. How do I get it to read and output all the settings.php file contents?

<?php 
$config = array("os"=>"windows","directory"=>"../Core-Stack/Themes/","ignore"=>array('_vti_cnf','cgi-bin','.','..'));

function getSettings($dir, $issubdir = false) { global $config, $SETTINGS;
if ($config['os'] == "unix") $delimiter = "/"; else if ($config['os'] == "windows") $delimiter = "\\";
if (!file_exists($dir) || !is_dir($dir) || !is_readable($dir)) {
echo "Error: \"$dir\" is not a directory, or I cannot read it properly.";
return 0;
} else if ($od = opendir($dir)) {
while (($file = readdir($od)) !== false) {
if (!in_array($file, $config['ignore'])) {
$path = $dir . $delimiter . $file;
if (is_dir($path)) getSettings($path, true);
elseif (is_file($path) && $file == "settings.php") include ($path);
}
}
closedir($od);
}
}

getSettings($config['directory'], true);
echo "Theme Name: ";
echo $SETTINGS['theme_name'];
echo "<br>";
echo "Theme Creator: ";
echo $SETTINGS['theme_creator'];
echo "<br>";
echo "Theme Version: ";
echo $SETTINGS['theme_version'];
echo "<br>";
echo "Theme Creation Date: ";
echo $SETTINGS['theme_creation_date'];
?>

    Minor flaw maybe:

    while (($file = readdir($od)) !== false) { 
    while (($file = readdir($od)) != false) { 

    I can't see anything wrong with your recursion, what's the purpose of the boolean?

      I wrote this a while back. not too terribly sure...

        Indent your code. It's horrible to read and an error can easily be hidden somewhere due to an if being nested when it shouldn't or something similar.

        It should indeed be

        ($file = readdir($od)) !== false
        

        Readdir will return either a filename, or boolean false. If readdir returns filename '0' you'd otherwise have a problem, since 0 casts to boolean false, thus

        # false
        if (0)
        
        # false
        if (0 != false)
        
        # true
        if (0 !== false)
        

        As for things being included or not, one thing that may cause issues is your use of global variables. Perhaps something inside an included file is not in global scope and that screws it up. Anyway, you could go through all your settings.php files and on line 1 insert

        echo __file__ . ' included';  # or error_log()
        

        That way you definitely get to see if a file is included or not.

        You might also want to add output / error logging of the current directory being scanned and once you're certain that it goes through all directories you thought it would, output / error log the files it goes through in each directory.

        And to get rid of the global mess start by adding $config as a third parameter to getSettings. If the only variables your included files define that need to be used after inclusion is the $SETTINGS array, you could simply return $SETTINGS in each of your settings files and

        
        Also, since you never use $issubdir, you could simply drop this function parameter.

          So I setup 2 settings.php files and they both where recognized when I used "echo file . ' included'; # or error_log()", but only one is outputted. which means it probably has something to do with the readdir function.

            Sorry didn't mean to mention the readdir function. but rather than using includes statements, maybe i should write a readfile function

              include (and require) does the exact same thing as copy pasting the contents of the included onto the line where the include statement is (replacing it), with one single difference: an included file can return a value, just like a function can. So if something is supposed to be sent as output, it really has to be sent as output, no matter where you write that code. Obviously both files ARE included, and the code IS executed, and sine you now put "echo ..." in those files, output really IS sent as well. But if you send no output, what would you expect to be sent as output?

              2 file approach, using include.
              This is file1.php

              echo 'I am file 1. This is sent as output due to the echo statement.';
              
              include 'file2.php';
              
              echo 'I am still file 1. Here is more output.';
              

              And file2.php

              $i = 2;
              

              And now, the exact same thing as one single file

              echo 'I am file 1. This is sent as output due to the echo statement.';
              
              $i = 2;
              
              echo 'I am still file 1. Here is more output.';
              

              No output from file2 in the 2 file approach. No output between the first and last line in the 1 file approach. Unexpected?

                Write a Reply...