简体   繁体   English

从ROOT网址查找路径

[英]Finding the path from the ROOT url

I have file say config.php which is stored in a user defined folder like /abc/def/config.php 我有说config.php的文件,它存储在用户定义的文件夹中,例如/abc/def/config.php

Now this file is included by another file like /test.php and this file is called in the browser. 现在,此文件包含在另一个文件中,例如/test.php,并且在浏览器中将调用此文件。

Now I want to find the path of the config.php from the root site. 现在,我想从根站点中找到config.php的路径。 Usually we can use SCRIPT_FILENAME. 通常我们可以使用SCRIPT_FILENAME。 But that is not possible because that will be related to test.php and not config.php. 但这是不可能的,因为那将与test.php而不是config.php相关。

So I call http://www.abc.com/test.php which includes http://www.abc.com/abc/def/config.php and I want the following: "/abc/def/" 所以,我呼吁http://www.abc.com/test.php包括http://www.abc.com/abc/def/config.php ,我想以下内容:“/ ABC / DEF /”

How do I get this? 我怎么得到这个?

Thank you for your time. 感谢您的时间。

There is nothing special about the folder abc/def as far as the web server is concerned. 就Web服务器而言,文件夹abc/def没有什么特别的。 As a consequence, there's no way to get to this part, other than hard-coding it in your PHP code. 结果,除了在您的PHP代码中进行硬编码之外,没有其他方法可以使用。 For that reason it's a good idea to always store the config.php file in the root directory, or in a configuration folder relative to the root directory. 因此,始终将config.php文件存储在根目录或相对于根目录的配置文件夹中是一个好主意。

On some servers you may use $_SERVER['DOCUMENT_ROOT'] to get the root document directory of the webserver. 在某些服务器上,您可以使用$_SERVER['DOCUMENT_ROOT']获取Web服务器的根文档目录。

You could do like this: 您可以这样:

$include_file_path = "/abc/def/config.php";  
include($include_file_path);

... the $include_file_path variable will now hold the path to your file. ... $ include_file_path变量现在将保存文件的路径。

Even better, you can use a default location for config files used by your applications (/myapp/config/config.php). 更好的是,您可以为应用程序使用的配置文件使用默认位置(/myapp/config/config.php)。 This way you always know where to look for config files. 这样,您始终知道在哪里查找配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM