1

我在另一个文件(带有一堆其他函数)中有一个 PHP 函数,它返回一个文件的地址。调用文件 commonFunctions.php。见下文。

function filePath($fPath) {
    $cutPath = str_replace("/var/www","http://myinternalwebsite",$fPath);
    return $cutPath;
}

当我想要另一个文件的地址时,我只需通过使用include $_SERVER['DOCUMENT_ROOT'] . "/commonFunctions.php和调用来包含我的函数文件filePath(dirname(__FILE__))。结果类似于http://myinternalwebsite.com/myScript.php.

有没有办法放置dirname(__FILE__)在我的函数中,所以我所要做的就是通过键入 filePath() 来引用该函数?我尝试做类似以下的事情

function filePath() {
    $cutPath = str_replace("/var/www","http://myinternalwebsite",dirname(__FILE__));
    return $cutPath;
}

但这显然给了我 commonFunctions.php 文件的地址,而不是我在其中调用函数的文件。

提前致谢。

-安东尼

4

1 回答 1

1

为什么不使用这样的东西?

$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];

这会输出带有文件的完整 URL,如下所示:

如果 URL 是http://website.com/section/index.php 它返回website.com/section/index.php

如果 URL 是http://website.com/section/ 它返回website.com/section/index.php

我的意思是,据我了解,您可能不需要函数。

于 2015-01-08T19:10:40.987 回答