/ in the beginning of a link to get to the root folder doesn't work in php include.
/在一个链接的开头,到达根文件夹在php include中不起作用。
for example "/example/example.php"
例如“/example/example.php”
What is the solution?
解决办法是什么?
7 个解决方案
#1
52
I'm assuming by root folder you mean your web document root, rather than filesystem root.
我假设根文件夹是指您的Web文档根目录,而不是文件系统根目录。
To that end, you can either
为此,你也可以
- add the web root folder to the include path, and
include('example/example.php')
- 将web根文件夹添加到包含路径,并包含('example / example.php')
- or you can
include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')
- 或者你可以包括($ _ SERVER ['DOCUMENT_ROOT']。'/ example / example.php')
#2
26
I had this issue too. Paul Dixon's answer is correct, but maybe this will help you understand why:
我也有这个问题。保罗迪克森的答案是正确的,但也许这将有助于你理解为什么:
The issue here is that PHP is a server side language. Where pure HTML documents can access files based on the root url you set up on the server (ie. to access an image from any sub-directory you're on you would use /images/example.jpg
to go from the top directory down), PHP actually accesses the server root when you use include (/images/example.jpg)
这里的问题是PHP是一种服务器端语言。纯HTML文档可以根据您在服务器上设置的根URL访问文件(即,从您所在的任何子目录访问图像,您将使用/images/example.jpg从顶层目录下载),当您使用include时,PHP实际访问服务器根目录(/images/example.jpg)
The site structure that you have set up actually lies within a file system in the Apache Server. My site root looks something like this, starting from the server root and going down:
您设置的站点结构实际上位于Apache Server中的文件系统中。我的站点root看起来像这样,从服务器根开始向下:
/home2/siteuserftp/public_html/test/
"test" represents your site root
“test”代表您的站点根目录
So to answer your question why your PHP include isn't getting the result you want (it is working exactly as it should) is because you're asking the PHP code to try and find your file at the server root, when it is actually located at the HTML root of your site which would look something like the above.
那么回答你的问题为什么你的PHP包含没有得到你想要的结果(它完全正常工作)是因为你要求PHP代码尝试在服务器根目录找到你的文件,当它实际上是位于您网站的HTML根目录,如上所示。
Your file would be based on the site root of "test/" and would look something like this:
您的文件将基于“test /”的站点根目录,并且看起来像这样:
/home2/siteuserftp/public_html/test/about/index.php
/home2/siteuserftp/public_html/test/about/index.php
The answer Paul Dixon provided:
Paul Dixon提供的答案是:
include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')
is exactly what will fix your problem (don't worry about trying to find the document root to replace 'DOCUMENT_ROOT', PHP will do it for you. Just make sure you have 'DOCUMENT_ROOT' literally in there)
正是解决问题的方法(不要担心尝试找到文档根来替换'DOCUMENT_ROOT',PHP会为你做。确保你确实在那里有'DOCUMENT_ROOT')
EDIT:
编辑:
More information DOCUMENT_ROOT and other PHP SERVER variables can be found here
更多信息可以在此处找到DOCUMENT_ROOT和其他PHP SERVER变量
#3
5
include() (and many other functions like require(), fopen(), etc) all work off the local filesystem, not the web root.
include()(和许多其他函数,如require(),fopen()等)都可以在本地文件系统上工作,而不是web根。
So, when you do something like this
所以,当你做这样的事情
include( "/example/example.php" );
You're trying to include from the root of your *nix machine.
您试图从* nix机器的根目录中包含。
And while there are a multitude of ways to approach what you're doing, Paul Dixon's suggestions are probably your best bets.
虽然有很多方法可以解决你正在做的事情,但保罗迪克森的建议可能是你最好的选择。
#4
2
I solved this on a machine running Windows and IIS with the following:
我在运行Windows和IIS的计算机上解决了这个问题,具体如下:
<?php
$docroot = 'http://www.example.com/';
include ($docroot.'inc-header.php');
?>
If you're on a local dev machine, you can force your domain to point to localhost by adding the following in C:\Windows\System32\drivers\etc\hosts
如果您使用的是本地开发计算机,则可以通过在C:\ Windows \ System32 \ drivers \ etc \ hosts中添加以下内容来强制您的域指向localhost
127.0.0.1 www.example.com
Also, you'll need to enable allow_url_include in php.ini like so
此外,您需要在php.ini中启用allow_url_include,就像这样
allow_url_include = On
#5
1
Every web server has a public_html
folder, in which you usually keep your files etc. By using /
, you will not get to public_html
, instead you direct towards the main (unaccesible) root. So, use $_SERVER['DOCUMENT_ROOT']."/your/locati.on"
instead
每个Web服务器都有一个public_html文件夹,您通常会在其中保存文件等。使用/,您将无法访问public_html,而是指向主(不可访问)根目录。因此,请使用$ _SERVER ['DOCUMENT_ROOT']。“/ your / locati.on”
#6
0
For me, the following trick worked. I'm using Windows with IIS, so DOCROOT is C:\Inetpub\wwwroot.
对我来说,以下技巧奏效了。我正在使用带有IIS的Windows,因此DOCROOT是C:\ Inetpub \ wwwroot。
- do subst of C:\Inetpub\wwwroot to a drive. Let it be W: (WEB contents).
- 将C:\ Inetpub \ wwwroot替换为驱动器。让它成为W :( WEB内容)。
subst W: C:\Inetpub\wwwroot
- edit php.ini this way: append W:\ to include_path, change doc_root to W:\
- 以这种方式编辑php.ini:将W:\附加到include_path,将doc_root更改为W:\
include_path = ".;c:\php\active\includes;W:\" doc_root = W:\
- put subst command into CMD file within Startup folder to make mapping automatically.
- 将subst命令放入Startup文件夹中的CMD文件中以自动进行映射。
Now, both versions allowed:
现在,两个版本都允许:
include '/common/common.inc'; // access to mapped W: root include 'common/common.inc'; // access to W: within include_path
#7
0
some versions of PHP may have the delimiter at the end of document root while others may not. As a practical matter you may want to use:
某些版本的PHP可能在文档根目录末尾有分隔符,而其他版本可能没有。作为一个实际问题,您可能想要使用:
$r = trim(filter_input(INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_STRING));
if (substr($r, 0, 1) == '/')
{
define("PATCH_SEPARATOR", "/");
}
else
{
define("PATCH_SEPARATOR", "\\");
}
if (substr($r, -1) == PATCH_SEPARATOR)
{
include_once ($r . 'example/example.php');
}
else
{
include_once ($r . PATCH_SEPARATOR . 'example/example.php');
}
#1
52
I'm assuming by root folder you mean your web document root, rather than filesystem root.
我假设根文件夹是指您的Web文档根目录,而不是文件系统根目录。
To that end, you can either
为此,你也可以
- add the web root folder to the include path, and
include('example/example.php')
- 将web根文件夹添加到包含路径,并包含('example / example.php')
- or you can
include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')
- 或者你可以包括($ _ SERVER ['DOCUMENT_ROOT']。'/ example / example.php')
#2
26
I had this issue too. Paul Dixon's answer is correct, but maybe this will help you understand why:
我也有这个问题。保罗迪克森的答案是正确的,但也许这将有助于你理解为什么:
The issue here is that PHP is a server side language. Where pure HTML documents can access files based on the root url you set up on the server (ie. to access an image from any sub-directory you're on you would use /images/example.jpg
to go from the top directory down), PHP actually accesses the server root when you use include (/images/example.jpg)
这里的问题是PHP是一种服务器端语言。纯HTML文档可以根据您在服务器上设置的根URL访问文件(即,从您所在的任何子目录访问图像,您将使用/images/example.jpg从顶层目录下载),当您使用include时,PHP实际访问服务器根目录(/images/example.jpg)
The site structure that you have set up actually lies within a file system in the Apache Server. My site root looks something like this, starting from the server root and going down:
您设置的站点结构实际上位于Apache Server中的文件系统中。我的站点root看起来像这样,从服务器根开始向下:
/home2/siteuserftp/public_html/test/
"test" represents your site root
“test”代表您的站点根目录
So to answer your question why your PHP include isn't getting the result you want (it is working exactly as it should) is because you're asking the PHP code to try and find your file at the server root, when it is actually located at the HTML root of your site which would look something like the above.
那么回答你的问题为什么你的PHP包含没有得到你想要的结果(它完全正常工作)是因为你要求PHP代码尝试在服务器根目录找到你的文件,当它实际上是位于您网站的HTML根目录,如上所示。
Your file would be based on the site root of "test/" and would look something like this:
您的文件将基于“test /”的站点根目录,并且看起来像这样:
/home2/siteuserftp/public_html/test/about/index.php
/home2/siteuserftp/public_html/test/about/index.php
The answer Paul Dixon provided:
Paul Dixon提供的答案是:
include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')
is exactly what will fix your problem (don't worry about trying to find the document root to replace 'DOCUMENT_ROOT', PHP will do it for you. Just make sure you have 'DOCUMENT_ROOT' literally in there)
正是解决问题的方法(不要担心尝试找到文档根来替换'DOCUMENT_ROOT',PHP会为你做。确保你确实在那里有'DOCUMENT_ROOT')
EDIT:
编辑:
More information DOCUMENT_ROOT and other PHP SERVER variables can be found here
更多信息可以在此处找到DOCUMENT_ROOT和其他PHP SERVER变量
#3
5
include() (and many other functions like require(), fopen(), etc) all work off the local filesystem, not the web root.
include()(和许多其他函数,如require(),fopen()等)都可以在本地文件系统上工作,而不是web根。
So, when you do something like this
所以,当你做这样的事情
include( "/example/example.php" );
You're trying to include from the root of your *nix machine.
您试图从* nix机器的根目录中包含。
And while there are a multitude of ways to approach what you're doing, Paul Dixon's suggestions are probably your best bets.
虽然有很多方法可以解决你正在做的事情,但保罗迪克森的建议可能是你最好的选择。
#4
2
I solved this on a machine running Windows and IIS with the following:
我在运行Windows和IIS的计算机上解决了这个问题,具体如下:
<?php
$docroot = 'http://www.example.com/';
include ($docroot.'inc-header.php');
?>
If you're on a local dev machine, you can force your domain to point to localhost by adding the following in C:\Windows\System32\drivers\etc\hosts
如果您使用的是本地开发计算机,则可以通过在C:\ Windows \ System32 \ drivers \ etc \ hosts中添加以下内容来强制您的域指向localhost
127.0.0.1 www.example.com
Also, you'll need to enable allow_url_include in php.ini like so
此外,您需要在php.ini中启用allow_url_include,就像这样
allow_url_include = On
#5
1
Every web server has a public_html
folder, in which you usually keep your files etc. By using /
, you will not get to public_html
, instead you direct towards the main (unaccesible) root. So, use $_SERVER['DOCUMENT_ROOT']."/your/locati.on"
instead
每个Web服务器都有一个public_html文件夹,您通常会在其中保存文件等。使用/,您将无法访问public_html,而是指向主(不可访问)根目录。因此,请使用$ _SERVER ['DOCUMENT_ROOT']。“/ your / locati.on”
#6
0
For me, the following trick worked. I'm using Windows with IIS, so DOCROOT is C:\Inetpub\wwwroot.
对我来说,以下技巧奏效了。我正在使用带有IIS的Windows,因此DOCROOT是C:\ Inetpub \ wwwroot。
- do subst of C:\Inetpub\wwwroot to a drive. Let it be W: (WEB contents).
- 将C:\ Inetpub \ wwwroot替换为驱动器。让它成为W :( WEB内容)。
subst W: C:\Inetpub\wwwroot
- edit php.ini this way: append W:\ to include_path, change doc_root to W:\
- 以这种方式编辑php.ini:将W:\附加到include_path,将doc_root更改为W:\
include_path = ".;c:\php\active\includes;W:\" doc_root = W:\
- put subst command into CMD file within Startup folder to make mapping automatically.
- 将subst命令放入Startup文件夹中的CMD文件中以自动进行映射。
Now, both versions allowed:
现在,两个版本都允许:
include '/common/common.inc'; // access to mapped W: root include 'common/common.inc'; // access to W: within include_path
#7
0
some versions of PHP may have the delimiter at the end of document root while others may not. As a practical matter you may want to use:
某些版本的PHP可能在文档根目录末尾有分隔符,而其他版本可能没有。作为一个实际问题,您可能想要使用:
$r = trim(filter_input(INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_STRING));
if (substr($r, 0, 1) == '/')
{
define("PATCH_SEPARATOR", "/");
}
else
{
define("PATCH_SEPARATOR", "\\");
}
if (substr($r, -1) == PATCH_SEPARATOR)
{
include_once ($r . 'example/example.php');
}
else
{
include_once ($r . PATCH_SEPARATOR . 'example/example.php');
}