为什么包含路径只在没有“/”的情况下起作用?
Why Include path works only without "/" in start?
我希望 index.php
包含位于 include
文件夹中的 footer.html
文件,该文件夹与我的 index.php
所在的文件夹相同。
我注意到如果我使用它是行不通的:
<?php include '/include/footer.html'; >
我会得到:
Warning: include(/include/footer.html): failed to open stream: No such
file or directory in
它只有在我使用时才有效:
<?php include 'include/footer.html'; >
这令人困惑,因为与此同时,我确实使用第一种形式来包含脚本和图像:
<script src="/js/script.js"></script>
<img src="/images/logo.jpg">
顺便说一句,将所有包含的文件集中在指定文件夹中更好吗?
令人困惑的是 PHP 的 include()
根据服务器本身的物理文件路径查找文件。它与您的网址无关。
你可以这样做:
<?php include './include/footer.html'; >
或:
<?php include 'include/footer.html'; >
或:
<?php include __DIR__ . '/include/footer.html'; >
如果您 <?php echo __DIR__ . '/include/footer.html'; ?>
,您将在磁盘上看到 footer.html
的完整路径。在Linux/UNIX系统上,/
是系统的根目录,所以当你使用/include/footer.html
时,它会在系统根目录中寻找一个名为include
的目录,并找到一个footer.html
里面。
我希望 index.php
包含位于 include
文件夹中的 footer.html
文件,该文件夹与我的 index.php
所在的文件夹相同。
我注意到如果我使用它是行不通的:
<?php include '/include/footer.html'; >
我会得到:
Warning: include(/include/footer.html): failed to open stream: No such file or directory in
它只有在我使用时才有效:
<?php include 'include/footer.html'; >
这令人困惑,因为与此同时,我确实使用第一种形式来包含脚本和图像:
<script src="/js/script.js"></script>
<img src="/images/logo.jpg">
顺便说一句,将所有包含的文件集中在指定文件夹中更好吗?
令人困惑的是 PHP 的 include()
根据服务器本身的物理文件路径查找文件。它与您的网址无关。
你可以这样做:
<?php include './include/footer.html'; >
或:
<?php include 'include/footer.html'; >
或:
<?php include __DIR__ . '/include/footer.html'; >
如果您 <?php echo __DIR__ . '/include/footer.html'; ?>
,您将在磁盘上看到 footer.html
的完整路径。在Linux/UNIX系统上,/
是系统的根目录,所以当你使用/include/footer.html
时,它会在系统根目录中寻找一个名为include
的目录,并找到一个footer.html
里面。