我用 HTML 编写了一个网站,但后来我想将页眉和导航栏部分放在一个文件中,所以我只需要更改一次即可。我将所有页面更改为 .php 并添加了 php 包含。但是,现在我在放入文件夹的页面上出现错误,因此我可以创建简洁的链接。
头文件:/test/assets/header.php
作品:/test/index.php
包含 <?php include 'assets/header.php'; ?>
抛出错误:/test/company/index.php
包含 <?php include '/test/assets/header.php'; ?>
错误:
Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
我在链接到根文件夹中某个文件夹中的头文件时遇到问题。我认为这是一个不知道如何输入 URL 的简单问题。如果我尝试包含 header.php 的完整路径,我会得到一个 URL file-access is disabled
错误
最佳答案
如果路径以 /
开头,那么它就是绝对路径。实际文件系统根目录的绝对主目录,而不是虚拟 document root
.这就是它失败的原因。
通常是这个意思:
include "$_SERVER[DOCUMENT_ROOT]/test/assets/header.php";
// Note: array key quotes only absent in double quotes context
关于PHP 包含 - 无法打开流 : No such file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8689577/