如何格式化 PHP include() 绝对(而不是相对)路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/344823/
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
How do I format a PHP include() absolute (rather than relative) path?
提问by Zack Peterson
On various pages throughout my PHP web site and in various nested directories I want to include a specific file at a path relative to the root.
在我的 PHP 网站的各个页面和各个嵌套目录中,我想在相对于根的路径中包含一个特定文件。
What single command can I put on both of these pages...
我可以在这两个页面上放置哪个命令...
http://www.example.com/pageone.php
http://www.example.com/somedirectory/pagetwo.php
...to include this page:
...包含此页面:
http://www.example.com/includes/analytics.php
This does not work:
这不起作用:
<?php include('/includes/analytics.php'); ?>
Does it matter that this is hosted in IIS on Windows?
在 Windows 上托管在 IIS 中是否重要?
采纳答案by gnud
If you give include() or require() (or the *_once versions) an absolute pathname, that file will be included. An absolute pathname starts with a "/" on unix, and with a drive letter and colon on Windows.
如果您给 include() 或 require() (或 *_once 版本)一个绝对路径名,该文件将被包含在内。绝对路径名在 unix 上以“/”开头,在 Windows 上以驱动器号和冒号开头。
If you give a relative path (any other path), PHP will search the directories in the configuration value "include_path" in order, until a match is found or there are no more directories to search.
如果您给出相对路径(任何其他路径),PHP 将按顺序搜索配置值“include_path”中的目录,直到找到匹配项或没有更多目录可供搜索。
So, in short, to include an absolute filename, give an absolute filename. See also the function realpath().
因此,简而言之,要包含绝对文件名,请给出绝对文件名。另请参阅函数realpath()。
If you want to set your own include "root", have a look at this question(specifically my answer of course :-)
如果您想设置自己的包含“root”,请查看此问题(当然特别是我的回答:-)
回答by J Cooper
You can just use include $_SERVER['DOCUMENT_ROOT'] . "/includes/analytics.php";
你可以使用 include $_SERVER['DOCUMENT_ROOT'] . "/includes/analytics.php";
回答by Zoredache
From the perspective of PHP root is the top of the file system on the web server, not the root from the perspective of the web browser.
从 PHP 的角度来看,root 是 Web 服务器上文件系统的顶层,而不是从 Web 浏览器的角度来看的 root。
Most people do one of the below.
大多数人会执行以下操作之一。
Define a constant, in a global configuration file, and use that in each call to require/include.
在全局配置文件中定义一个常量,并在每次调用 require/include 时使用它。
Or they use code like this.
或者他们使用这样的代码。
require_once realpath(dirname(__FILE__).'/config.php');
require_once realpath(dirname(__FILE__).'/lib/Database.php');
Using the environmental variables may be dangerous in some cases and be the source of security issues.
在某些情况下,使用环境变量可能是危险的,并且是安全问题的根源。
回答by Greg
As @Stefan Mai says, PHP doesn't have a "root" path but you can define one quite easily - most sites have a page that's included every time (e.g. configuration file), to which you can add:
正如@Stefan Mai 所说,PHP 没有“根”路径,但您可以很容易地定义一个路径——大多数站点都有一个每次都包含的页面(例如配置文件),您可以向其中添加:
define('ROOT', dirname(__FILE__));
Then use include ROOT . '/includes/analytics.php';
然后使用 include ROOT 。'/includes/analytics.php';
Something that's also quite useful is the auto_prepend directive, which you can use in .htaccess on apache - not sure about setting it up on IIS (although you can have a global one in the PHP ini).
auto_prepend 指令也非常有用,您可以在 apache 上的 .htaccess 中使用它 - 不确定是否在 IIS 上设置它(尽管您可以在 PHP ini 中有一个全局的)。
回答by Reggie Z. Banal
I combined the above suggestions and came up with this.
我结合了上述建议并提出了这一点。
In my config.phpfile, I define ROOT as:
在我的config.php文件中,我将 ROOT 定义为:
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
Then I use it in my succeding files:
然后我在我的后续文件中使用它:
include_once(ROOT."/someFile.php");
include_once(ROOT."/someFile.php");
include_once(ROOT."/includes/someFile.php");
include_once(ROOT."/includes/someFile.php");
回答by Zack Peterson
This works.
这有效。
<?php include('c:/inetpub/example.com/includes/analytics.php'); ?>
I'll have to make the c:/inetpub/example.com/part some kind of global variable so it's somewhat portable from server to server.
我必须使该c:/inetpub/example.com/部分成为某种全局变量,以便它在服务器之间具有某种程度的可移植性。
回答by Stefan Mai
Use .. to go up a directory. So in pageone.php
使用 .. 上一个目录。所以在 pageone.php
include 'includes/analytics.php';
in pagetwo.php
在 pagetwo.php 中
include '../includes/analytics.php';
There's no notion of "root" as you're referring to in PHP as far as I know, though you could define it if you wanted. Best of luck!
据我所知,没有您在 PHP 中所指的“root”的概念,尽管您可以根据需要定义它。祝你好运!
回答by Stefan Mai
The tilde is interpreted as a special character by the shell, so using it inside PHP code won't work regardless of OS.
波浪号被 shell 解释为一个特殊字符,因此无论操作系统如何,在 PHP 代码中使用它都不起作用。
If you're trying to access something relative to a user home directory you could try getenv()- I'm pretty sure Windows sets an environment variable equivalent to $HOME.
如果您尝试访问与用户主目录相关的内容,您可以尝试getenv()- 我很确定 Windows 设置了一个等效于$HOME.
回答by Santiago
The way I found is:
我找到的方法是:
<?php include $_SERVER['DOCUMENT_ROOT'].'\Your\Site\Path.php' ?>
The \\ is necesary when you have some special character that could be escaped (i.e. \n), so that \\ is \ on the string.
当您有一些可以转义的特殊字符(即 \n)时,\\ 是必需的,因此 \\ 是字符串上的 \。
Because you are in windows, you have to use \ instead of /
因为你在windows中,你必须使用\而不是/

