PHP递归目录处理问题

发布于 2024-11-03 17:29:44 字数 1438 浏览 0 评论 0原文

getSettings() 似乎只读取并输出目录中的 1 个 settings.php 文件。如何让它读取并输出所有settings.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']; 
?>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2024-11-10 17:29:44

您应该在递归之前存储目录内容,您的 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);
    }

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);
    }
不语却知心 2024-11-10 17:29:44

您的“设置”文件很可能以某种方式定义设置,例如 $SETTINGS = array(...);,当然,这样您只会看到最新包含文件中的内容。在不重新制作整个事情的情况下,您可以在这里做的事情是:
不更改settings.php

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

或者如果您可以更改settings.php文件:

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


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

当然,如果我理解您的意图的话。如果没有 - 那么请编辑您的问题并添加更多详细信息。

更新

您还可以使用scandir将函数放入更少的行中,并在树非常深的情况下防止堆的潜在问题,如下所示:

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

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));
        }
    } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文