开发者

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

Now this file is included by another file like /test.php and this file is called in the browser.

Now I want to find the path of the config.php from the root site. Usually we can use SCRIPT_FILENAME. But that is not开发者_C百科 possible because that will be related to test.php and not 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/"

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. As a consequence, there’s no way to get to this part, other than hard-coding it in your PHP code. 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.

On some servers you may use $_SERVER['DOCUMENT_ROOT'] to get the root document directory of the webserver.


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.

Even better, you can use a default location for config files used by your applications (/myapp/config/config.php). This way you always know where to look for config files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜