简体   繁体   English

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

[英]how to include some php file to another folder using include

I am using include or include_once in php core project 我在php核心项目中使用include或include_once

include('test1/footer.php'); this is working in root folder .. 这是工作在根文件夹..

but we using above code in anyfolder like name test2 and include test1 file in test2 here above code not working.. include('test1/footer.php'); 但是我们在诸如名称test2之类的任何文件夹中使用上述代码,并在test2中包含test1文件,此处上述代码不起作用.. include('test1/footer.php');

please help me . 请帮我 。

thanks.. 谢谢..

folder structure 文件夹结构

/root
    /test1
        footer.php
    index.php

index.php index.php

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

folder structure 文件夹结构

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

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');

Try this, 尝试这个,

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

But both test1 and test2 on same level. 但是test1和test2处于同一级别。

http://prntscr.com/8ho3gk http://prntscr.com/8ho3gk

check this screen shot to understand structure 检查此屏幕快照以了解结构

if you want to add "configs.php" in "cargo.php" write this include('include/configs.php'); 如果要在“ cargo.php”中添加“ configs.php”,请写这个include('include / configs.php'); and if you want to add "cargo.php" in "configs.php" write this include('../cargo.php'); 如果要在“ configs.php”中添加“ cargo.php”,请编写此include('../ cargo.php');

better use include_once(); 更好地使用include_once(); not only include(); 不仅包括();

best practice for me is to define a constant containing server ROOT path (or other path that is base for your includes) 对我而言,最佳实践是定义一个包含服务器ROOT路径(或包含在内的其他路径)的常量

add code to root/index.php or to a configuration file if you have one 将代码添加到root/index.php或配置文件(如果有)

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

use it anywhere in the project code, its also good when you change files hierarchy: 在项目代码中的任何地方都可以使用它,当您更改文件层次结构时,它也很好:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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