简体   繁体   中英

Can't include a file via PHP

I am uploading a navigation bar called nav.html and I would like to use PHP 7 to include this file at the top of all my pages.

This code: <?php include '/home/u783318484/public_html/nav.html';?> returns the error message,

Warning: include(/home/u783318484/public_html/nav.html) : failed to open stream: No such file or directory in /home/u783318484/public_html/apply.php on line 24

Warning: include(): Failed opening '/home/u783318484/public_html/nav.html' for inclusion (include_path='.:/opt/php-7.0/pear') in /home/u783318484/public_html/apply.php on line 24

It works fine on my PC with Apache, but when I upload it to my website (hosted by Hostinger) it does this. The nav.html is in the same folder as the other pages.

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

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

Use this:

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

Or you could use:

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

You can't include ABSOLUTE path from your root of your file system. This is for a very easy reason: when you upload your php to your server, will your server know your path? The answer is absolutelly NO.

For this reason you have to include your files as RELATIVE paths.

Take a look here: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/

Answering your question you must to this

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

or if you want that this file works in every path

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM