簡體   English   中英

在PHP中包含后獲取當前文件名

[英]Get current file name after include in PHP

假設我有以下class.wrapper.php

// loop through events/ directory ($eventFile = filename of parsed file)
include $eventFile;

myevent.php

echo __FILE__;

myotherevent.php

echo __FILE__;

兩個回聲都返回相同的東西: class.wrapper.php ,有沒有一種方法可以獲取真實的文件名? 沒有包含代碼的代碼嗎?

class.wrapper是加密API的一部分,該API允許用戶在給定目錄中定義自定義php文件,以便在主應用程序中觸發給定事件時調用用戶定義的代碼,因此我無法編輯主類,我必須這樣做這只能通過編輯“事件”文件

試試下面的功能

function GetInclude($file, $params = array())
    {
        extract($params);
        include $file;
    }
    myevent.php
    <?php GetInclude('class.wrapper.php', array('includerFile' => __FILE__)); ?>
    myotherevent.php
    <?php GetInclude('class.wrapper.php', array('includerFile' => __FILE__)); ?>
    class.wrapper.php
    echo $includerFile;

如果要獲取真實名稱(例如,獲取該文件的完整路徑),請使用函數realpath("yourfile.extension")

我舉一個簡短的例子:

假設我有一個名為index.php的文件。 我想顯示其完整路徑。 這是這樣做的代碼:

<?php
    echo realpath("index.php");
?>

在我的示例中,我將文件保存在C:\\ xampp \\ htdocs \\ test \\ index.php中。 結果將顯示此確切路徑。

希望能幫助到你。

暫無
暫無

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

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