获取原始文件夹的路径而不是其中的路径

时间:2017-03-24 23:44:21

标签: php function include require filepath

我目前正在开发一个PHP项目,我遇到了一个无法找到解决方案的问题。

这是项目的外观:

  1. INC
    • 的functions.php
    • 的index.php
  2. functions.php中我有一个特定的函数,它将提供当前的工作路径。 当我像{<1}} index.php一样加入

    functions.php

    然后调用我的函数include '../INC/functions.php'; 它返回INC的路径而不是它包含和使用的路径?我怎样才能找到我想要的路径?

1 个答案:

答案 0 :(得分:1)

您可以使用$_SERVER["SCRIPT_FILENAME"]。 想象一下,我们有这个文件结构:

  • folder1 / index.php
  • 文件夹2 /的functions.php

<强>的folder1 / index.php的:

<?php
include('../folder2/functions.php');

getCurrent();

<强>文件夹2 /的functions.php

<?php
function getCurrent(){
    echo 'Current file:'.$_SERVER["SCRIPT_FILENAME"];
}

如果你打开folder1 / index.php,你会得到(例如):

  

当前档案:/folder1/index.php

相关问题