[英]how to include some php file to another folder using include
我在php核心項目中使用include或include_once
include('test1/footer.php');
這是工作在根文件夾..
但是我們在諸如名稱test2之類的任何文件夾中使用上述代碼,並在test2中包含test1文件,此處上述代碼不起作用.. include('test1/footer.php');
請幫我 。
謝謝..
文件夾結構
/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');
嘗試這個,
include('../test1/footer.php');
但是test1和test2處於同一級別。
檢查此屏幕快照以了解結構
如果要在“ cargo.php”中添加“ configs.php”,請寫這個include('include / configs.php'); 如果要在“ configs.php”中添加“ cargo.php”,請編寫此include('../ cargo.php');
更好地使用include_once(); 不僅包括();
對我而言,最佳實踐是定義一個包含服務器ROOT路徑(或包含在內的其他路徑)的常量
將代碼添加到root/index.php
或配置文件(如果有)
define('MYROOT', dirname(__FILE__));
在項目代碼中的任何地方都可以使用它,當您更改文件層次結構時,它也很好:
include(MYROOT.'test1/footer.php');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.