PHP递归目录处理问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在递归之前存储目录内容,您的 else-if 块应该如下所示:
You should store directory contents before recursion, your else-if block should be like this:
您的“设置”文件很可能以某种方式定义设置,例如
$SETTINGS = array(...);
,当然,这样您只会看到最新包含文件中的内容。在不重新制作整个事情的情况下,您可以在这里做的事情是:不更改
settings.php
:或者如果您可以更改
settings.php
文件:当然,如果我理解您的意图的话。如果没有 - 那么请编辑您的问题并添加更多详细信息。
更新
您还可以使用
scandir
将函数放入更少的行中,并在树非常深的情况下防止堆的潜在问题,如下所示: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
:or if you can change the
settings.php
files: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: