簡體   English   中英

php:包含數組中定義的文件時失敗

[英]php: failing when including a file that is defined in an array

我想基於從數據庫中獲取的值包括一個文件。 在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)"; 它什么也不做(是的yes $ elements [module]只包含單詞“ tax”)。 我什至嘗試將$ 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擴展名之前的斜杠和點被忽略了。 感謝您的幫助:)

如果我使用include(“ modules / tax.php)”; 它工作正常。

在您的代碼中,您正在使用

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

嘗試刪除/

您正在查看是否存在"modules/".$page."php" ,但嘗試包含"/modules/".$page."php"可能不是相同的東西(請注意前導/ )。

if(file_exists("modules/".$page."php")) include("modules/".$page."php");

好的,我已經解決了,謝謝:)每個答案都是有幫助的:)(答案是您的答案加上我需要更多注意的組合。我忘記了.php擴展名之前的..lolz ..猜測您何時累了,您必須先說句小詞,才能正確表達自己的觀點)。 再次感謝你們:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM