不能通过 PHP 包含文件


Can't include a file via PHP

我正在上传一个名为nav.html的导航栏,我想使用 PHP 7 将这个文件include到我所有页面的顶部。

此代码:<?php include '/home/u783318484/public_html/nav.html';?>返回错误消息,

警告:include(/home/u783318484/public_html/nav.html):无法打开流:第 24 行的/home/u783318484/public_html/apply.php 中没有此类文件或目录

警告:(include_path='.:/opt/php-7.0/pear') include(): Failed opening '/home/u783318484/public_html/nav.html'包含在 /home/u783318484/public_html/apply.php 第 24 行的/home/u783318484/

它在带有Apache的PC上运行良好,但是当我将其上传到我的网站(由Hostinger托管(时,它会这样做。nav.html与其他页面位于同一文件夹中。

检查您的路径并尝试使用此

<?php include(dirname(__FILE__).'/nav.html');?>

使用这个:

<?php include ('./nav.html'); ?>

或者您可以使用:

<?php require ('./nav.html'); ?>

不能包含文件系统根目录的绝对路径。原因很简单:当你把你的php上传到你的服务器时,你的服务器会知道你的路径吗?答案绝对是否定的。

因此,您必须将文件作为相对路径包含在内。

看看这里: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/

回答你的问题,你必须这样做

<?php include('nav.html');?>

或者如果您希望此文件在每个路径中都有效

<?php include(dirname(__FILE__) . "/nav.html"); ?>