遇到问题 - $_SERVER['SERVER_NAME']
我知道我正在做一些愚蠢且可能显而易见的事情,但我不确定是什么。在我的 header.php
中,我有:
define('ABSPATH', $_SERVER['SERVER_NAME']);
require_once(ABSPATH . "/config.php");
使用它会产生以下错误:
Warning: require_once(localhost/config.php): failed to open stream:
No such file or directory in C:\wamp\www\inc\header.php on line 4
Call Stack: 0.0002 684176 1. {main}() C:\wamp\www\index.php:0 0.0003
696152 2. require_once('C:\wamp\www\inc\header.php') C:\wamp\www\index.php:2
Fatal error: require_once(): Failed opening required 'localhost/config.php' (include_path='.;C:\php\pear') in C:\wamp\www\inc\header.php on line 4 Call Stack:
0.0002 684176 1. {main}() C:\wamp\www\index.php:0 0.0003 696152 2. require_once('C:\wamp\www\inc\header.php') C:\wamp\www\index.php:2
但是,如果我直接导航到 localhost/config.php
(目前,它只是一个简单的 1 行回显句子的文件)它会起作用。
我做错了什么?
I know I'm doing something stupid and probably obvious, but I'm not sure what. In my header.php
I have:
define('ABSPATH', $_SERVER['SERVER_NAME']);
require_once(ABSPATH . "/config.php");
Using this produces the following error:
Warning: require_once(localhost/config.php): failed to open stream:
No such file or directory in C:\wamp\www\inc\header.php on line 4
Call Stack: 0.0002 684176 1. {main}() C:\wamp\www\index.php:0 0.0003
696152 2. require_once('C:\wamp\www\inc\header.php') C:\wamp\www\index.php:2
Fatal error: require_once(): Failed opening required 'localhost/config.php' (include_path='.;C:\php\pear') in C:\wamp\www\inc\header.php on line 4 Call Stack:
0.0002 684176 1. {main}() C:\wamp\www\index.php:0 0.0003 696152 2. require_once('C:\wamp\www\inc\header.php') C:\wamp\www\index.php:2
However, if I navigate directly to localhost/config.php
(currently, it's just a simple 1 line file that is echoing a sentence) It will work.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
$_SERVER['DOCUMENT_ROOT']
应该可以解决问题$_SERVER['DOCUMENT_ROOT']
should do the trickinclude
以及从服务器看到的与文件一起使用的任何其他内容都与文件系统路径一起使用。C:\foo\bar
是文件系统路径。localhost
不是,localhost
是从网络中看到的 URL 的主机名。文件localhost/config.php
在被视为网络上的 URL 时有效。本地系统上的同一文件具有类似C:\wamp\www\config.php
的路径,这是您需要使用require_once
它的路径。include
s and anything else that works with files as seen from the server work with file system paths.C:\foo\bar
is a file system path.localhost
is not,localhost
is the hostname of a URL as seen from the network. The filelocalhost/config.php
is valid when treated as a URL over a network. The same file on the local system has a path likeC:\wamp\www\config.php
, which is what you need to use torequire_once
it.您确定您不是想在 Windows 系统上使用反斜杠吗?
另外,请确保该文件位于 Web 根目录中的
localhost
文件夹中。Are you sure you didn't mean to use a backslash on your Windows system, there?
Also, make sure the file is in the
localhost
folder within your web root.尝试以下:
Try below :