PHP包含绝对路径
我的网站上有一个名为 $basePath 的变量,它设置为:
I have a variable on my site called $basePath which is set as:
$basePath = '/Systems/dgw/';
我在我所有的 css、js 和图像标签上都使用它(为了更好的可见性而缩短):
I am using it on all my css, js and images tags as so (shortened for better visibility):
<link href="<?php echo $basePath; ?>include/assets/css/bootstrap.min.css">
我对这些包含没有任何问题,它们在任何文件和我所在的任何文件夹中都可以正常工作.
I have no problem with those includes and they work fine in wherever file and in whatever folder I am.
我有一个包含以下内容的页面:
I have a certain included page which has the following line:
<img src="<?php echo $basePath; ?>images/new_logo.png" alt="logo"/>
而且图像显示得很好.它后面的行是:
And the image shows just fine. The line after it states:
<?php include($basePath.'include/assets/common/topMessages.php');?>
但包含不会发生.当我这样尝试时:
But the include doesn't happens. When I try it like this:
<?php include('../../include/assets/common/topMessages.php');?>
有效.
有人知道哪里出了问题吗?
Anybody has any idea what could be wrong?
推荐答案
你不能这样包含相对于你的 webroot 的 php 文件,因为如果你使用斜杠作为第一个字符,引用将比你的更深文档根目录.因此,您可以执行以下操作,而不是使用基本路径:
You can't include php files relatively to your webroot that way, cause if you use the slash as first character, the reference will go much deeper than just your document root. So, instead of using your basepath, you could do something like this :
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/yourfile.php";
include_once($path);
?>
相关文章