recently i worked in a project. the project has one root directory named "Project".the "Project" directory has also two sub-directory named "project_a" and "project_b". the "project_b" directory has two more sub-directory named "project_b_1" and "project_b_2". when i am on a page which is contains "project_b_2" directory then how can i access a file which is exists in the "project_a" directory. all code must be php code.
最近我在一个项目工作。该项目有一个名为“Project”的根目录。“Project”目录还有两个名为“project_a”和“project_b”的子目录。 “project_b”目录还有两个名为“project_b_1”和“project_b_2”的子目录。当我在包含“project_b_2”目录的页面上时,如何访问“project_a”目录中存在的文件。所有代码必须是PHP代码。
3 个解决方案
#1
Using $_SERVER['DOCUMENT_ROOT']
will always reference to where the root is in your defined site/vhost etc.
So using something like this should work:
使用$ _SERVER ['DOCUMENT_ROOT']将始终引用root在您定义的站点/ vhost等中的位置。所以使用这样的东西应该有效:
fopen($_SERVER['DOCUMENT_ROOT'].'/projectA/subfileB.html');
#2
Find the path to 'project_a' relative to script in 'project_b_2':
在'project_b_2'中找到相对于脚本的'project_a'的路径:
$path = realpath( dirname( __FILE __ ) . '/../../project_a/' );
Find the path to 'project_a' relative to the webroot:
找到相对于webroot的'project_a'的路径:
$path = $_SERVER['DOCUMENT_ROOT'] . '/Project/project_a/';
#3
You should be able to go up in the directory tree :
你应该能够进入目录树:
#when you are in project/project_b/project_b_2
$dir = "../../project_a";
That is the way I usually follow, although you can use
这是我通常遵循的方式,尽管你可以使用
$_SERVER["SITE_HTMLROOT"]
which should take you to the root of the website
哪个应该带你到网站的根目录
#1
Using $_SERVER['DOCUMENT_ROOT']
will always reference to where the root is in your defined site/vhost etc.
So using something like this should work:
使用$ _SERVER ['DOCUMENT_ROOT']将始终引用root在您定义的站点/ vhost等中的位置。所以使用这样的东西应该有效:
fopen($_SERVER['DOCUMENT_ROOT'].'/projectA/subfileB.html');
#2
Find the path to 'project_a' relative to script in 'project_b_2':
在'project_b_2'中找到相对于脚本的'project_a'的路径:
$path = realpath( dirname( __FILE __ ) . '/../../project_a/' );
Find the path to 'project_a' relative to the webroot:
找到相对于webroot的'project_a'的路径:
$path = $_SERVER['DOCUMENT_ROOT'] . '/Project/project_a/';
#3
You should be able to go up in the directory tree :
你应该能够进入目录树:
#when you are in project/project_b/project_b_2
$dir = "../../project_a";
That is the way I usually follow, although you can use
这是我通常遵循的方式,尽管你可以使用
$_SERVER["SITE_HTMLROOT"]
which should take you to the root of the website
哪个应该带你到网站的根目录