在PHP中找到一个文件,该文件是4节目录了(finding a file in php that i

2019-07-01 14:43发布

使用多的时候我甚至无法找到该文件../来查找文件:

root -> public -> secure -> lock  -> login / "data.php"
                            panel -> data -> list / "list.php"

我想包括data.phplist.php ,并试着做以下操作,包括数据:

include("../../../../lock/login/data.php");
include("../../../lock/login/data.php");
and also
include("../../lock/login/data.php");

然而,这似乎并没有工作,但如果我把它放在旁边list.php ,并做到以下几点,它读取数据就好了:

include("data.php");

我在做什么错我的代码,什么是更好的方式来执行呢?

Answer 1:

了解如何使用

__FILE__; // for your current file path
dirname(__FILE__) or __DIR__; // for your current file parent folder
$_SERVER['DOCUMENT_ROOT']; // for your sites inception folder, your pivotal point

这应该让你对你的方式。 然后,您可以

include __DIR__.'/../file-REALLY-relative-to-directory-of-edit-file.php';
include $_SERVER['DOCUMENT_ROOT'].'/absolute-path-reliable-for-your-site.php';

从此再也相对包括像 (当前目录可以改变和打破你的世界):

include 'an-unreliable-NOT-REALLY-relative-file.php';

希望能帮助到你!



Answer 2:

Use phps include_path feature instead and specify those directories that contain php scripts to be included. Easier and safer.



文章来源: finding a file in php that is 4 directories up