php递归包含2个不同路径的文件中的函数

时间:2015-02-12 13:03:38

标签: php include

大家好我在php中的include函数问题:

我有4个文件:

dir1/file1.php
dir4/dir2/file2.php
dir3/file3.php
dir3/file4.php

在file1.php中我有:

include_once('../dir3/file3.php');

在file3.php中我有:

required('../dir3/file4.php');

在file2.php中我想写:

include_once('../../dir3/file3.php');

但file3中的所需函数不起作用,因为file4的路径必须

../../dir3/file4.php 

而不是

../dir3/file4.php

我该如何解决?

3 个答案:

答案 0 :(得分:1)

你可以在你的文件名之前只使用点(。),这将从dir的根目录找到该文件,例如./dir3/file4.php,但它会增加开销。另一种方法是使用

$base = __DIR__ . '/../';

require_once $base.'_include/file1.php';

答案 1 :(得分:0)

如果从file2调用file3,则必须返回2个目录。最好的方法是使用完整的路径,如:

home/mysite/public_html/dir3/file3.php

这可能是麻烦但很好,直到某种程度。 编辑: DIR ,休息也很方便,具体取决于您的需要

答案 2 :(得分:0)

我修复它:

在file1.php中我有:

$path = '..';    
include_once($path.'/dir3/file3.php');

在file3.php中我有:

required($path.'/dir3/file4.php');

在file2.php中我想写:

$path = '../..'
include_once($path.'/dir3/file3.php');

这项工作适合我。