将文件包含在与public_html并行的目录中

时间:2016-02-16 00:48:35

标签: php

我希望在我的路径中有一个动态值,其中包含一个位于public_html以外的目录中的文件。当我测试

echo dirname(__FILE__);

我得到以下结果:

/home/domain/subdomain/mydir

相同
/home/domain/public_html/mydir

这是我的开发服务器,网址为:subdomain.domain.com/mydir/file1

我想要包含的文件位于

/home/domain/include/anotherdir/file2

我的生产服务器具有不同的域,并且不使用子域: anotherdomain.com/mydir/file1 ,其解析为:

/home/anotherdomain/mydir/file1

有没有办法在不触及服务器设置的情况下使用PHP完成此动态路径?

1 个答案:

答案 0 :(得分:1)

if(!defined('MYHOMEROOT'))
{
    define('MYHOMEROOT',realpath(dirname(__FILE__).'/../../../'));
}
include(MYHOMEROOT.'/mydir1/file.php');

以上,请将'/../../../'更改为" ../"你需要后退的路径 您可以通过

确定
$testpath=realpath(dirname(__FILE__).'/../../../');
echo $testpath;

以上假设Linux 谢谢,