PHP include absolute path(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="Jmx0Oz9waHAgZWNobyAkYmFzZVBhdGg7ID8mZ3Q7aW1hZ2VzL25ld19sb2dvLnBuZw==" alt="bG9nbw=="/>
而且图像显示得很好.它后面的行是:
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包含绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP包含绝对路径


- 没有作曲家的 PSR4 自动加载 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 仓库 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01