本地主机上的目录名(__FILE__) [英] dirname(__FILE__) on localhost
问题描述
我正在使用WAMP,并且在www目录中有一个开发站点。我想使用 dirname(__ FILE __)
定义服务器根目录的路径。
I'm using WAMP and have a development site in the www directory. I want to use dirname(__FILE__)
to define the path to the server root.
当前我使用的配置文件包含:
Currently I'm using a config file which contains:
define('PATH', dirname(__FILE__));
我将配置文件包含在我的 header.php中
文件,如下所示:
I'm including the config file in in my header.php
file like this:
<?php require_once("config.php") ?>
然后,在子页面上,我使用常量 PATH
通过包含 header.php
来定义路径。
Then, on my sub pages I use the constant PATH
to define the path by including header.php
.
<?php require_once("../inc/header.php"); ?>
但是,我的链接如下所示:
However, my links are coming out like this:
<link rel="stylesheet" href="C:\wamp\www/css/style.css" />
我需要怎么做才能解决此问题?而且由于我将常量包含在 header.php
文件中,因此我无法访问初始 require_once(。中的常量。 /inc/header.php);
我还能用什么其他方法来找到 header.php
的根?
What do I need to do to fix this? And since I'm including my constant in the header.php
file I don't have access to the constant in the initial require_once("../inc/header.php");
What other method can I use to find the root for header.php
?
推荐答案
看起来您只需要拥有
define('PATH', $_SERVER['SERVER_NAME']);
如果您想成为超级技术人员,可以改成这样。
If you want to be super technical, you can do something like this instead.
define('PATH', str_replace($_SERVER['DOCUMENT_ROOT'], $_SERVER['SERVER_NAME'] . '/', dirname(__FILE__)));
另一方面,更重要的是,您实际上并没有需要他们。
On a side note, and more importantly, you don't actually need them. This will work.
<link rel="stylesheet" href="/css/style.css" />
当href以目录分隔符开头时,它被视为相对于文档根目录,而不是当前目录工作目录。
When a href begins with a directory separator, it is considered relative to the document root, not the current working directory.
这篇关于本地主机上的目录名(__FILE__)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!