[英]calling include from an included file
So, examining this directory structure 所以,检查这个目录结构
Assume that I am in /directory/main_file.php and I call include('../include_one.php');
假设我在/directory/main_file.php中 ,我调用
include('../include_one.php');
inside of include_one.php , to include include_two.php . 在include_one.php中 ,包括include_two.php 。 Do I need to call
include('include_two.php);
我需要调用
include('include_two.php);
or include('../include_two.php');
或
include('../include_two.php');
? ?
So my question is: When including a file, is the 'relative include path' shifted to the included file, or does it remain at the main including file? 所以我的问题是:当包含文件时,“相对包含路径”是否转移到包含文件,还是保留在主包含文件中?
I am aware that the best alternative would be to have a config.php which contains the root_path, however this is not possible at this stage. 我知道最好的选择是拥有一个包含root_path的config.php,但是在这个阶段这是不可能的。
directory structure 目录结构
/include.php /include.php
/start/start.php /start/start.php
/folder1/includeone.php /folder1/includeone.php
/folder1/folder2/includetwo.php /folder1/folder2/includetwo.php
and here is the contents of each file 这是每个文件的内容
start.php start.php
<?php echo 'including ../include.php<br />'; include('../include.php'); ?>
include.php include.php
<?php echo 'including folder1/includeone.php<br />'; include('folder1/includeone.php'); ?>
includeone.php includeone.php
<?php echo 'including folder2/includetwo.php<br />'; include('folder2/includetwo.php'); ?>
includetwo.php includetwo.php
<?php echo 'done<br />'; ?>
and the output is 而输出是
including ../include.php
包括../include.php
including folder1/includeone.php包括folder1 / includeone.php
including folder2/includetwo.php包括folder2 / includetwo.php
doneDONE
The "relative include path" is not shifted to the included file... Which means that using relative paths generally ends badly. “相对包含路径”不会转移到包含的文件...这意味着使用相对路径通常会很糟糕。
A better solution, that I use almost all the time, is to always work with absolute paths -- and you can mix relatives and absolute paths using __DIR__
, to get the directory that contains the file where this is written. 我几乎一直使用的更好的解决方案是始终使用绝对路径 - 您可以使用
__DIR__
混合亲戚和绝对路径,以获取包含写入此文件的文件的目录。
For example, in include_one.php
, you'd use : 例如,在
include_one.php
,您将使用:
require_once __DIR__ . '/include_two.php';
To include the include_two.php
file that's in the same directory as include_one.php
. 要包含与
include_two.php
位于同一目录中的include_one.php
。
And, in main_file.php
, you'd use : 并且,在
main_file.php
,您将使用:
require_once __DIR__ . '/../include_one.php';
To include the include_one.php
file that's one directory up. 要包含一个目录的
include_one.php
文件。
With that, your includes will work, no matter from which file they are called. 这样,无论从哪个文件中调用它们,您的包含都将起作用。
I am using this code: 我正在使用此代码:
if(!isset($TO_HOME_DIR)) $TO_HOME_DIR="../";
And I include a file: 我包含一个文件:
include_once($TO_HOME_DIR."common/include_one.php");
With if(!isset($TO_HOME_DIR))
, it's not important how much you include a file into included file into included file into includ..... Only first file's -and main file's- $TO_HOME_DIR declaration is used. 使用
if(!isset($TO_HOME_DIR))
,将包含文件中的文件包含到包含文件中的数量并不重要.......仅使用第一个文件的 - 和主文件 - $ TO_HOME_DIR声明。
Second advantage of this approach is, if you change directory of file, you only need to change one line of code; 这种方法的第二个优点是,如果你改变文件目录,你只需要改变一行代码; $TO_HOME_DIR declaration.
$ TO_HOME_DIR声明。 :)
:)
The include path is relative to the first file in the include chain. 包含路径相对于包含链中的第一个文件。
A good way to ensure the correct include path is to always include from the document root. 确保正确包含路径的一种好方法是始终包含文档根目录。
This is done like this: 这样做是这样的:
include $_SERVER['DOCUMENT_ROOT'] . '/folder1/folder2/includetwo.php';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.