gpt4 book ai didi

php:包含数组中定义的文件时失败

转载 作者:行者123 更新时间:2023-11-29 14:06:11 25 4
gpt4 key购买 nike

我想包含一个基于从数据库中获取的值的文件。在 page.php 的类页面内部我有这个函数来获取我想要的数据:

function getPage(){
$page=array(
'title'=>$this->title,
'content'=>$this->content,
'module'=>$this->module
);
return $page;
}

在 pageview.php 中我这样调用它: $elements=$page->getPage();该函数工作得很好,我得到了我的内容,我可以像这样操作它(我知道heredox不是要走的路,但请忽略它,这不是问题):

 echo <<<EOT
<div class='row-fluid'>
<H1> title:$elements[title]</H1></br>
$elements[content]
$elements[module]
</div>
EOT;

现在问题来了。我有一个名为 includeModule 的函数,如下所示:

function includeModule($page){

$page=strtolower($page);

if(file_exists("modules/".$page."php")) include("/modules/".$page."php");
else Echo "No such Module Exists :".$page." </br>";

}

现在假设我想包含一个名为“tax.php”的页面。如果我使用 include("/modules/tax.php)"; 它工作得很好。如果我尝试使用 include("/modules/".$elements[module].".php)"; 它什么也不做(是的 $elements[module] 确实只包含单词“税” )。我什至尝试将 $elements[module] 的值分配给另一个变量,但什么也没有。

最奇怪的部分是,如果我尝试使用我的函数(includeModule),即使我手动将 $page 变量设置为“tax”,它仍然不包含任何内容并表示该文件不存在(即使确实如此)?

对此有什么帮助吗?

编辑:我已经尝试删除/但如果我这样做,它会再次包含任何内容

编辑:我稍微更改了功能,以便获得一些反馈

if(file_exists("modules/".$page.".php")){
echo "<p>file exists</p>";
include("modules/".$page."**.**php");
}else{
echo getcwd();
Echo "<p>No such Module Exists :".$page."</p>";
}

好的解决了。感谢所有的答案。由于我的错误,最后的斜杠以及 php 扩展名之前的点被遗漏了。感谢您的帮助:)

最佳答案

if i use include("modules/tax.php)"; it works just fine.

在您的代码中,您使用的是

include("/modules/".$page."php");
^-- Note the leading /

尝试删除/

关于php:包含数组中定义的文件时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14306173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com