开发者

PHP Recursive Directory Handle Problem

开发者 https://www.devze.com 2023-03-01 12:59 出处:网络
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?

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']; 
?>


You should store directory contents before recursion, your else-if block should be like this:

else if ($od = opendir($dir)) { 
        $subdirs = array();
        while (($file = readdir($od)) !== false) { 
            if (!in_array($file, $config['ignore'])) { 
                $path = $dir . $delimiter . $file; 
                if (is_dir($path)) $subdirs[] = $path;
                elseif (is_file($path) && $file == "settings.php") include ($path); 
            }           
        }
        closedir($od);
        foreach($subdirs as $subdir)
            getSettings($subdir, true);
    }


Most likely your "settings" files are defining settings somehow like $SETTINGS = array(...); and of course that way you will only see contents from the latest included file. What you could do here without remaking the whole thing would be either: without changing settings.php:

//...
elseif (is_file($path) && $file == "settings.php") {
    $OLD_SETTINGS = $SETTINGS;
    include ($path);
    $SETTINGS = array_merge($OLD_SETTINGS, $SETTINGS);
}
//...

or if you can change the settings.php files:

//...
elseif (is_file($path) && $file == "settings.php") {
    $SETTINGS = array_merge($SETTINGS, include ($path));
}
//...


//----in settings.php
return array(
    'option' => 'foobar',
    //...
);

That's of course if I got your intensions right. If not - then please edit your question and add more details.

UPDATE

also you could use scandir to fit the function in less lines and prevent potential problems with heap if the tree is VERY deep, like this:

function getSettings($dir, $issubdir = false) { 
    global $config, $SETTINGS; 
    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 ($files = scandir($dir)) { 
        foreach ($files as $file) { 
            if (in_array($file, $config['ignore'])) continue;

            $path = $dir . DIRECTORY_SEPARATOR . $file; 
            if (is_dir($path)) 
                getSettings($path, true); 
            elseif (is_file($path) && $file == "settings.php") 
                $SETTINGS = array_merge($SETTINGS, include ($path));
        }
    } 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消