PHP:如何从用户目录中获取文档根? [英] PHP: How to get the document root from inside a user directory?

查看:188
本文介绍了PHP:如何从用户目录中获取文档根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发和部署各种PHP应用到不同的环境。特别是在开发环境,他们可以在任何地方,从DOCUMENT_ROOT到/用户/我/网站/或甚/用户/我/网站/ someapp /

I develop and deploy various PHP applications to different environments. Especially on development environments, they can be anywhere, from document_root to /Users/me/Sites/ or even /Users/me/Sites/someapp/

在这些应用中,我需要知道在哪里应用程序根目录',一旦作为真正的路径和一次作为URL。路径是没有问题的。比方说,我在应用程序根目录下它做了bootstrap.php中:

Inside these applications I need to know where the 'application root' is, once as the real path and once as URL. Path is no problem. Let's say I have a bootstrap.php in the app root directory which does:

define("BASE_DIR", realpath(dirname(__FILE__)));

不过,我有问题要得到可靠的基础URL。在大多数环境中简单地减去BASE_DIR文档根目录如下:

However, I have problems to reliably get the base URL. On most environments simply subtracting document root from BASE_DIR works:

define("BASE_URL", str_replace($_SERVER['DOCUMENT_ROOT'],'',BASE_DIR) . "/");

现在,我的问题是:在我的应用在于我的用户目录中,因为PHP仍然认为主文档根这并不对环境中工作。有没有人解决了这个问题?

Now, my problem is: This does not work on environments where my app lies inside my user directory because PHP still sees the main document root. Has anyone solved this problem?

推荐答案

任何涉及真实路径()和DOCUMENT_ROOT会配置服务器的别名得到时,发生故障的硬盘。考虑这样一个场景,Apache的有这样的配置:

Anything involving realpath() and DOCUMENT_ROOT is going to fail hard when the server's got aliases configured. Consider a scenario where Apache's got a configuration like this:

DocumentRoot /home/httpd/html
Alias /testalias /home/otherdir

和你 example.com/testalias/script.php 访问脚本。

该脚本将返回:

realpath(dirname(__FILE__)) -> /home/otherdir
$_SERVER['DOCUMENT_ROOT'] -> /home/httpd/html
BASE_DIR -> /home/otherdir
BASE_URL -> /home/otherdir/

,但该网站的其余部分确实存在 /家/的httpd / HTML

您可能有更好的运气重建基于 $网址_ SERVER ['SCRIPT_NAME'] ,这是URL的路径/脚本名称部分:

You might have better luck reconstructing the URL based on $_SERVER['SCRIPT_NAME'], which is the path/script name portion of the URL:

$_SERVER['SCRIPT_NAME'] -> /testalias/script.php

这篇关于PHP:如何从用户目录中获取文档根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆