如何在php文件中包含php文件 [英] How to include php file in a php file

查看:99
本文介绍了如何在php文件中包含php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想在我的网站中包含navigation.php文件,但我不知道如何在.php文件中包含外部文件,这是我可以找到的将Navigation.php包含到网站中的唯一方法文件是要在每个目录中对其进行复制.

Hey I would like to include a navigation.php file in my website, but i cant figure out how to include an external file in a .php file, and the only way i could find to include the navigation.php into the files was to make a copy of it in each directory.

<?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.

我只希望将这些navigation.php文件之一放在格式"目录中,然后使每个页面都提取并显示该文件.

I would like to only have one of these navigation.php files in a "Format" directory and then make each page fetch the file and display it.

还要一段时间监视这个问题,所以问我是否需要其他信息.

Also ill be monitoring this question for a while so just ask me if you need any additional info.

推荐答案

将该文件放置一个位置.然后在每个文件中使用其正确的路径.

Put that file one place. Then refer to it in each file by its proper 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"); ?>

但是,如果您在许多目录中有许多文件,这可能会变得很乏味.最好的方法是使用文件系统上文件的完整路径:

But this can get tedious if you have lots of files in lots of directories. The best way to do this is to use the full path to the file on the file system:

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

但是,如果路径更改,这可能会很乏味.因此,定义一个变量或常量来为您保留该值,因此您只需要在一个地方进行更改即可:

But this can get tedious if the path changes. So define a variable or constant to hold that value for you so you only need to change it in one place:

// 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"); ?>

这篇关于如何在php文件中包含php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆