php 确定网站的“根”位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7448944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determining a website's 'root' location
提问by Yarin
I need some help with concepts and terminology regarding website 'root' urls and directories.
我需要一些有关网站“根”网址和目录的概念和术语的帮助。
Is it possible to determine a website's root, or is that an arbitrary idea, and only the actual server's root can be established?
是否可以确定网站的根目录,或者这是一个随意的想法,并且只能建立实际服务器的根目录?
Let's say I'm writing a PHP plugin that that will be used by different websites in different locations, but needs to determine what the website's base directory is. Using PHP, I will always be able to determine the DOCUMENT_ROOT and SERVER_NAME, that is, the absolute URL and absolute directory path of the server (or virtual server). But I can't know if the website itself is 'installed' at the root directory or in a sub directory. If the website was in a subdirectory, I would need the user to explicitly set an "sub-path" variable. Correct?
假设我正在编写一个 PHP 插件,它将被不同位置的不同网站使用,但需要确定网站的基本目录是什么。使用PHP,我总是可以确定DOCUMENT_ROOT和SERVER_NAME,即服务器(或虚拟服务器)的绝对URL和绝对目录路径。但我不知道网站本身是“安装”在根目录还是子目录中。如果网站位于子目录中,我需要用户明确设置“子路径”变量。正确的?
回答by Imad Moqaddem
Answer to question 1: Yes you need a variable which explicitly sets the root path of the website. It can be done with an htaccess file at the root of each website containing the following line :
问题 1 的回答:是的,您需要一个明确设置网站根路径的变量。可以使用包含以下行的每个网站根目录下的 htaccess 文件来完成:
SetEnv APP_ROOT_PATH /path/to/app
http://httpd.apache.org/docs/2.0/mod/mod_env.html
http://httpd.apache.org/docs/2.0/mod/mod_env.html
And you can access it anywhere in your php script by using :
您可以使用以下命令在 php 脚本中的任何位置访问它:
<?php $appRootPath = getenv('APP_ROOT_PATH'); ?>
回答by genesis
Will $url and $dir always be pointing to the same place?
$url 和 $dir 总是指向同一个地方吗?
<?php
$some_relative_path = "hello";
$server_url = $_SERVER["SERVER_NAME"];
$doc_root = $_SERVER["DOCUMENT_ROOT"];
echo $url = $server_url.'/'. $some_relative_path."<br />";
echo $dir = $doc_root.'/'. $some_relative_path;
Output:
输出:
sandbox.phpcode.eu/hello
/data/sandbox//hello
回答by Dayo
You shouldn't need to ask the user to provide any info.
您不需要要求用户提供任何信息。
This snippet will let your code know whether it is running in the root or not:
此代码段将让您的代码知道它是否在根目录中运行:
<?php
// Load the absolute server path to the directory the script is running in
$fileDir = dirname(__FILE__);
// Make sure we end with a slash
if (substr($fileDir, -1) != '/') {
$fileDir .= '/';
}
// Load the absolute server path to the document root
$docRoot = $_SERVER['DOCUMENT_ROOT'];
// Make sure we end with a slash
if (substr($docRoot, -1) != '/') {
$docRoot .= '/';
}
// Remove docRoot string from fileDir string as subPath string
$subPath = preg_replace('~' . $docRoot . '~i', '', $fileDir);
// Add a slash to the beginning of subPath string
$subPath = '/' . $subPath;
// Test subPath string to determine if we are in the web root or not
if ($subPath == '/') {
// if subPath = single slash, docRoot and fileDir strings were the same
echo "We are running in the web foot folder of http://" . $_SERVER['SERVER_NAME'];
} else {
// Anyting else means the file is running in a subdirectory
echo "We are running in the '" . $subPath . "' subdirectory of http://" . $_SERVER['SERVER_NAME'];
}
?>
回答by Foo-Wing Li
I have just had the same problem. I wanted to reference links and other files from the root directory of my website structure.
我刚刚遇到了同样的问题。我想从我的网站结构的根目录中引用链接和其他文件。
I tried the following but it would never work how I wanted it:
我尝试了以下操作,但它永远不会像我想要的那样工作:
$root = $_SERVER['DOCUMENT_ROOT'];
echo "<a href="' . $root . '/index.php">Link</a>";
echo "<a href="' . $root . '/admin/index.php">Link</a>";
But the obvious solution was just to use the following:
但显而易见的解决方案只是使用以下内容:
echo "<a href="../index.php">Link</a>";
echo "<a href="../admin/index.php">Link</a>";