如何以编程方式确定在PHP文档根目录? [英] How to programmatically determine the document root in PHP?

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

问题描述

下面是我一直运行到最近的一个问题 - 在一个虚拟主机提供商配置错误的Apache。这意味着,依靠 $ _ SERVER ['DOCUMENT_ROOT'] 打破所有的脚本。我发现最简单的解决方法是刚刚设置一些全局变量包括共享的文件,但它是一个痛苦,不要忘记它。我的问题是,我该如何确定正确的文档根目录编程?

Here's a problem that I've been running into lately - a misconfigured apache on a webhost. This means that all scripts that rely on $_SERVER['DOCUMENT_ROOT'] break. The easiest workaround that I've found is just set the variable in some global include files that is shared, but it's a pain not to forget it. My question is, how do I determine the correct document root programatically?

例如,一台主机上,设置是这样的:

For example, on one host, the setup is like this:

$_SERVER['DOCUMENT_ROOT'] == '/htdocs'

真正的文档根目录是:

The real document roots are:

test.example.com -> /data/htdocs/example.com/test
www.example.com -> /data/htdocs/example.com/www

和我想这是一个从 www.example.com/blog / 运行脚本(路径 /数据/ htdocs中/例如上.COM / WWW /博客),以获得正确的价值 /data/htdocs/example.com/www

And I'd like a script that's run from www.example.com/blog/ (on the path /data/htdocs/example.com/www/blog) to get the correct value of /data/htdocs/example.com/www.

在另一台主机,安装有一点不同:

On another host, the setup is a bit different:

$_SERVER['DOCUMENT_ROOT'] == '/srv'
test.example.com -> /home/virtual_web/example.com/public_html/test
www.example.com -> /home/virtual_web/example.com/public_html/www

有没有解决这个?或者是唯一的出路根本就没有永远依靠 $ _ SERVER ['DOCUMENT_ROOT'] 并解决我在我的网站上运行的所有软件?修复此上托管的身边似乎没有成为一种选择,我还没有遇到这何处是正确配置的主机。我得到了最好的是文档根目录指向www.example.com,这至少是里面的open_basedir - 他们用另一种命名方式,www.example.com将指向 / U2 / WWW / example_com /数据/网络/

Is there any solution to this? Or is the only way simply not to ever rely on $_SERVER['DOCUMENT_ROOT'] and fix all the software that I'm running on my sites? Fixing this on the hosting's side doesn't seem to be an option, I've yet to encounter a host where this is was configured correctly. The best I got was a document root pointing to www.example.com, which was at least inside open_basedir - they used yet another naming scheme, www.example.com would point to /u2/www/example_com/data/www/.

推荐答案

根据 HTTP://www.helicron .NET / PHP /

$localpath=getenv("SCRIPT_NAME");
$absolutepath=getenv("SCRIPT_FILENAME");
$_SERVER['DOCUMENT_ROOT']=substr($absolutepath,0,strpos($absolutepath,$localpath));     

我不得不改变基本名/真实路径把戏,因为这回我的主机上一个空字符串。相反,我使用 SCRIPT_FILENAME 。这可能不会在IIS上工作了(不过那可能使用的$ _SERVER变量的原创剧本也不会觉得要)。

I had to change the basename/realpath trick because it returned an empty string on my host. Instead, I use SCRIPT_FILENAME. This probably won't work on IIS anymore (but the original scripts that used the $_SERVER variable probably wouldn't either).

这篇关于如何以编程方式确定在PHP文档根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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