PHP getcwd() 函数与包含或需要一起使用在不同的目录中


PHP getcwd() function to be used with include or require in different directories

我需要将配置文件的路径包含在其他 scrpits 的一个目录中,向上或向下几个目录。

我的配置文件有这样的功能:

$execute_path = getcwd() . "/execute.php";

execute.php 文件与配置文件位于同一目录中,使其正常,输出将变为:

public-html/config-directory/execute.php

现在,如果在原始目录之外的目录中使用相同的函数,它将 getcwd() 函数视为在该目录中。

例如,如果从目录 public-html/one/two/three/somefile 调用它.php如下所示:

<?php 
include(pathto/config.php)
echo $execute_path;
?>

输出变为 publichtml/one/two/three/execute.php而不是保持 publichtml/config-directory/execute.php

怎样才能在这里实现我想要的?对不起,解释太长了。谢谢。

包含可能非常棘手,为什么我的建议是

//config.php
define("SITE_PATH", "public-html/config-directory");

//execute.php
$execute_path =SITE_PATH . "/execute.php";
//run.php
include_once 'config.php';
include_once 'execute.php';
echo $execute_path;