如何获取 public_html 文件夹的绝对路径

How to get absolute path to public_html folder

我在搜索问题时对绝对路径有点困惑。我有一个 php 项目,其中包含一个 Header.php 文件,该文件是第一级 php 文件的 header。为避免添加另一个 header,我也想让它适用于内部关卡。例如,在项目的另一个文件夹中包含的 php 文件中使用它。 为此,我想获取我的 public_html 文件夹的绝对路径并在我的 Header.php 中使用它。我怎样才能得到它?

开头使用/

include('/header.php');

这将始终包括 root 文件夹中的 header.php

你可以通过这种方式得到你当前脚本文件的绝对路径:

$absolute_path = dirname(__FILE__);

您可以在此处查看所有 Magic 常量:http://php.net/manual/en/language.constants.predefined.php

上面的

None 在不同的服务器上对我有用,所以必须编写以下函数:

echo  "public_html absolute path:".public_html();
function public_html() {
  $path=getcwd();
  if (!strpos($path,'public_html')) $path=$_SERVER['PHP_SELF'];
  $awords= explode('/', $path); 
  $public_html="";
  foreach ($awords as  $word)   {
     $public_html.=$word."/";
     if ($word=='public_html') break;
  }
return  substr($public_html,0,-1);
}

我想你需要的是 $_SERVER['DOCUMENT_ROOT']