根路径不适用于 php include
/ 在链接开头获取根文件夹在 php include 中不起作用。
例如“/example/example.php”
解决方案是什么?
/ in the beginning of a link to get to the root folder doesn't work in php include.
for example "/example/example.php"
What is the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我假设根文件夹是指您的网络文档根目录,而不是文件系统根目录。
为此,您可以
include('example/example.php')
include($_SERVER['DOCUMENT_ROOT'].'/example/example.php ')
I'm assuming by root folder you mean your web document root, rather than filesystem root.
To that end, you can either
include('example/example.php')
include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')
我也有这个问题。 Paul Dixon 的答案是正确的,但这也许可以帮助您理解原因:
这里的问题是 PHP 是一种服务器端语言。纯 HTML 文档可以根据您在服务器上设置的根 url 访问文件(即,要从您所在的任何子目录访问图像,您可以使用
/images/example.jpg
从顶部目录向下),当您使用include (/images/example.jpg)
时,PHP 实际上访问服务器根目录。您设置的站点结构实际上位于阿帕奇服务器。我的站点根目录看起来像这样,从服务器根目录开始并向下:
“test”代表您的站点根目录
所以回答您的问题为什么您的 PHP 包含没有得到您想要的结果(它完全按照应有的方式工作)是因为您要求 PHP 代码尝试在服务器根目录中查找您的文件,而它实际上位于站点的 HTML 根目录中,类似于上面的内容。
您的文件将基于“test/”的站点根目录,看起来像这样:
/home2/siteuserftp/public_html/test/about/index.php
Paul Dixon 提供的答案:
正是解决您问题的方法(不要不必担心尝试找到文档根目录来替换“DOCUMENT_ROOT”,PHP 会为您完成此操作,只需确保其中确实有“DOCUMENT_ROOT”)
编辑:
更多信息 DOCUMENT_ROOT 和其他 PHP服务器变量可以在这里找到
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 useinclude (/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:
"test" represents your site root
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.
Your file would be based on the site root of "test/" and would look something like this:
/home2/siteuserftp/public_html/test/about/index.php
The answer Paul Dixon provided:
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)
EDIT:
More information DOCUMENT_ROOT and other PHP SERVER variables can be found here
include()(以及许多其他函数,如 require()、fopen() 等)都在本地文件系统上工作,而不是在 Web 根目录下工作。
所以,当你做这样的事情时,
你试图从你的 *nix 机器的根目录包含。
虽然有多种方法可以完成您正在做的事情,但保罗·迪克森的建议可能是您最好的选择。
include() (and many other functions like require(), fopen(), etc) all work off the local filesystem, not the web root.
So, when you do something like this
You're trying to include from the root of your *nix machine.
And while there are a multitude of ways to approach what you're doing, Paul Dixon's suggestions are probably your best bets.
每个网络服务器都有一个
public_html
文件夹,您通常在其中保存文件等。通过使用/
,您将不会到达public_html
,而是您直接指向主(无法访问)根。因此,请使用$_SERVER['DOCUMENT_ROOT']."/your/locati.on"
Every web server has a
public_html
folder, in which you usually keep your files etc. By using/
, you will not get topublic_html
, instead you direct towards the main (unaccesible) root. So, use$_SERVER['DOCUMENT_ROOT']."/your/locati.on"
instead我在运行 Windows 和 IIS 的计算机上通过以下方法解决了这个问题:
如果您在本地开发计算机上,则可以通过在 C:\Windows\System32\drivers\etc\hosts 中添加以下内容来强制您的域指向 localhost
另外,您需要在 php.ini 中启用allow_url_include,如下所示
I solved this on a machine running Windows and IIS with the following:
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
Also, you'll need to enable allow_url_include in php.ini like so
对我来说,以下技巧奏效了。
我使用带有 IIS 的 Windows,因此 DOCROOT 是 C:\Inetpub\wwwroot。
现在,两个版本都允许:
For me, the following trick worked.
I'm using Windows with IIS, so DOCROOT is C:\Inetpub\wwwroot.
Now, both versions allowed:
某些版本的 PHP 可能在文档根目录末尾有分隔符,而其他版本可能没有。作为实际问题,您可能想要使用:
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:
也许这有点不传统,
如果我有一个像
我使用的解决方案很简单的情况。
获取“Dir1”之前的路径,
像这样
我发现它很有用,我需要重命名主子目录
例如从
TO
maybe it's a bit unconventional
If I have a case like
the solution that I use is simple.
get the path before the "Dir1"
something like this
I found it usefull id i need to rename the main subdir
for example from
TO
这个答案并不是真正针对根目录,但一种解决方法是使用
../
跳转到父目录。当然,您需要了解这种方法的文件结构.
例如,您可以使用:
This answer is not really for the root directory, but one workaround is to use
../
to jump to the parent directory.Of course, you need to know the file structure for this approach though.
For example, you could use: