问题描述
服务器上的Apache版本:2.2.26
服务器上的 PHP 版本:5.5.9
我在一个文件夹中有一个名为 admin_config.php
的文件,其中有一个 .htaccess
文件,其中 Deny from ALL
值.>
从 index.php
(在 DOCUMENT_ROOT
)我试图用下面的行包含这个文件:
include '/core/config/admin_config.php'
在家里,这很好用.在我的云服务器上,这不起作用(没有错误消息).如果我改用 require
,则会收到内部 500 错误.
如果我将行更改为相对路径,它会起作用:
include 'core/config/admin_config.php'
为什么这里不能设置绝对路径?这是服务器上的一些错误,还是我的错误
您提供给 include
或 require
的路径是本地文件系统.它们不是您在访问您网站的 URL 中看到的内容.以 /
开头的绝对路径来自文件系统的根目录.在 Windows 术语中,/foo
是 C:\foo\
.像 foo/bar
这样的相对路径是相对于 PATH
配置变量的,这取决于你的 PATH
是如何设置的,它也包括哪些 PHP文件被调用.
使用绝对路径通常不是一个好主意,因为它们在不同的系统上可能会有所不同(如您所见).在您的本地机器上,该站点可能位于 C:\core\...
,但在服务器上,它将运行在 /var/www/mysite/core/...代码>.
PATH
使用起来也很麻烦.最好的方法通常是使用 __DIR__
或 __FILE__
魔术常量来构造相对于当前文件的绝对路径(如果有意义的话):
需要 __DIR__ .'/some/folder/file.php`;
这包括相对于写入它的文件的文件 some/folder/file.php
.
Apache version on server: 2.2.26
PHP version on server: 5.5.9
I have a file called admin_config.php
in a folder, which has an .htaccess
file with Deny from ALL
value.
From index.php
(at DOCUMENT_ROOT
) I'm trying to include this file with the following line:
include '/core/config/admin_config.php'
At home, this works fine. On my cloud server, this doesn't work (no error message). If I use require
instead, I get an internal 500 error.
If I change the line to a relative path, it works:
include 'core/config/admin_config.php'
Why can't I set an absolute path here? Is this some bug on the server, or an error on my part
Paths you give to include
or require
are paths on the local filesystem. They are not what you see in URLs to access your site. An absolute path starting with /
is from the root of the filesystem. In Windows terms, /foo
is C:\foo\
. Relative paths like foo/bar
are relative to the the PATH
configuration variable, which depends on how your PATH
is set up which also includes which PHP file was invoked.
It's typically not a good idea to use absolute paths, since those are likely different on different systems (as you are experiencing). On your local machine the site may live in C:\core\...
, but on the server it'll be running in /var/www/mysite/core/...
. PATH
s can also be cumbersome to work with. The best is typically to use __DIR__
or __FILE__
magic constants to construct an absolute path relative to the current file (if that made sense):
require __DIR__ . '/some/folder/file.php`;
This includes the file some/folder/file.php
relative to the file in which it is written.
这篇关于不能包含或要求绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!