简体   繁体   English

PHP 递归目录句柄问题

[英]PHP Recursive Directory Handle Problem

getSettings() seems to only read and output 1 settings.php file in the directory. getSettings() 似乎只读取目录中的 output 1 settings.php 文件。 How do I get it to read and output all the settings.php file contents?如何让它读取 output 所有设置。php 文件内容?

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

Most likely your "settings" files are defining settings somehow like $SETTINGS = array(...);很可能您的“设置”文件正在以某种方式定义设置,例如$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 :如果不重新制作整个事情,你可以在这里做的是:不更改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:或者如果您可以更改settings.php文件:

//...
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:您也可以使用scandir以更少的行来安装 function 并防止在树非常深时出现堆的潜在问题,如下所示:

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));
        }
    } 
} 

You should store directory contents before recursion, your else-if block should be like this:您应该在递归之前存储目录内容,您的 else-if 块应该是这样的:

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM