问题描述
我的网站上有一个名为$ 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);
?>
这篇关于PHP包括绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!