如何使用include将一些php文件包含到另一个文件夹中

时间:2015-09-18 10:07:22

标签: php

我在php核心项目中使用include或include_once

include('test1/footer.php');这适用于根文件夹..

但是我们在任何文件夹中使用上面的代码,比如名称test2,并在test2中包含test1文件,上面的代码不起作用.. include('test1/footer.php');

请帮帮我。

感谢..

4 个答案:

答案 0 :(得分:1)

文件夹结构

/root
    /test1
        footer.php
    index.php

<强>的index.php

//works fine because test1 is on the same level as index.php
include('test1/footer.php');

文件夹结构

/root
    /test1
        footer.php
    /test2
        index.php

<强>的index.php

//does not work since in our current folder there is no folder "test1"
include('test1/footer.php');
//does work since we go back to root first
include('../test1/footer.php');

答案 1 :(得分:0)

试试这个,

include('../test1/footer.php');

但test1和test2都处于同一水平。

答案 2 :(得分:0)

http://prntscr.com/8ho3gk

检查此屏幕截图以了解结构

如果你想在“cargo.php”中添加“configs.php” 写这个包括('include / configs.php'); 和 如果你想在“configs.php”中添加“cargo.php” 写这个包括('../ cargo.php');

更好地使用include_once();不仅包括();

答案 3 :(得分:0)

对我来说,最佳做法是定义一个包含服务器ROOT路径的常量 (或其他基础适用于您的包含)

添加代码 root/index.php或配置文件(如果有)

define('MYROOT', dirname(__FILE__));

在项目代码中的任何地方使用它,当你更改文件层次结构时它也很好:

include(MYROOT.'test1/footer.php');
相关问题