【问题标题】:Using a function in another file to find a file's path/address in PHP使用另一个文件中的函数在 PHP 中查找文件的路径/地址
【发布时间】:2015-01-08 17:43:07
【问题描述】:

我在另一个文件(带有一堆其他函数)中有一个 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 文件的地址,而不是我在其中调用函数的文件。

提前致谢。

-安东尼

【问题讨论】:

  • 没有办法按照您的要求进行操作。如果您正在尝试解决更普遍的问题,也许说明该问题会激发某人能够对替代方法发表评论。

标签: php function filepath dirname


【解决方案1】:

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

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

这会输出完整的 URL 和文件,如下:

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

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

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

【讨论】:

  • 我同意这个。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 2022-08-19
  • 2017-06-02
  • 2021-01-17
相关资源
最近更新 更多