简体   繁体   中英

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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