如何在php文件中包含php文件

时间:2014-05-01 01:09:32

标签: php file

嘿我想在我的网站中包含一个navigation.php文件,但我无法弄清楚如何在.php文件中包含一个外部文件,这是我能找到的唯一方法将navigation.php包含在文件是在每个目录中复制它。

<?php include("navigation.php"); ?> works when i make a copy of the navigation.php in each directory.


<?php include("http://www.mysite.net/format/navigation.php"); ?> gives me an error.

我想在“Format”目录中只有一个这样的navigation.php文件,然后让每个页面获取文件并显示它。

也有人监视这个问题一段时间,所以请问我是否需要任何其他信息。

1 个答案:

答案 0 :(得分:2)

将该文件放在一个处。然后通过适当的path在每个文件中引用它。

// navigation.php is in the same directory
<?php include("navigation.php"); ?>
<?php include("./navigation.php"); ?>

// navigation.php is one directory below the current directory
<?php include("../navigation.php"); ?>

// navigation.php is two directories below the current directory
<?php include("../../navigation.php"); ?>

但如果你在很多目录中有很多文件,这可能会变得乏味。执行此操作的最佳方法是使用文件系统上文件的完整路径:

// Full path relative to the root of the account
<?php include("/full/path/to/navigation.php"); ?>

但如果路径改变,这可能会变得乏味。因此,定义一个变量或常量来保存该值,这样您只需要在一个地方更改它:

// In a config file
define('INCLUDE_PATH', '/full/path/to/');

// In each page after you include your config file
<?php include(INCLUDE_PATH . "navigation.php"); ?>