eazyGen Posted September 29, 2011 Share Posted September 29, 2011 Hi guys, I am searching for the definitive way of obtaining the root directory of my app. I need to it to work on localhost and also on ALL platforms, NIX, Windows etc. I would like to do this as I wish to use it to locate all my sub directory files - includes, model etc. For example, I may have this as my landing page: http://www.mywebsite.com/index.php The root directory might be: html The index.php might reside here: html/index.php ... but my includes files might be here: html/lib/includes So I would be looking to obtain the root in an absolute kind of way to allow me to string together the includes sub directory in a relative kind of way. In summary then: 1. How to obtain the root with 100% percent success, all of the time on all platforms. 2. How to string the sub directory together with 100% percent success, all of the time on all platforms. 3. Any comments on this as an approach, also welcome. Many thanks in advance, S Quote Link to comment https://forums.phpfreaks.com/topic/248089-definitive-way-of-obtaining-the-root-directory/ Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 You might find what you are looking for here Particularly, the DOCUMENT_ROOT index. Quote Link to comment https://forums.phpfreaks.com/topic/248089-definitive-way-of-obtaining-the-root-directory/#findComment-1273892 Share on other sites More sharing options...
eazyGen Posted September 29, 2011 Author Share Posted September 29, 2011 You might find what you are looking for here Particularly, the DOCUMENT_ROOT index. Many thanks. However, this seems to highlight the problem I am having and the reason for my post. The link has two statements that concern me: $HTTP_SERVER_VARS [deprecated] ... so I cannot use that. "$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these ..." I am loathe therefore to pursue a solution that carries no guarantees. Any further thoughts much appreciated. S Quote Link to comment https://forums.phpfreaks.com/topic/248089-definitive-way-of-obtaining-the-root-directory/#findComment-1273896 Share on other sites More sharing options...
Muddy_Funster Posted September 29, 2011 Share Posted September 29, 2011 The only way you are ever going to guarentee anything is if you have direct admin access to the server. Basicly, the software can only get the information that the server will let it access, unless you controll that server, there can be no guarantees. Quote Link to comment https://forums.phpfreaks.com/topic/248089-definitive-way-of-obtaining-the-root-directory/#findComment-1273904 Share on other sites More sharing options...
eazyGen Posted September 29, 2011 Author Share Posted September 29, 2011 The only way you are ever going to guarentee anything is if you have direct admin access to the server. Basicly, the software can only get the information that the server will let it access, unless you controll that server, there can be no guarantees. If that is the case, and I must say I find it slightly odd that something so apparently simple should not come with a guarantee, is there a "best practice" to achieve what I am aiming for? Perhaps a settings.ini file??? S Quote Link to comment https://forums.phpfreaks.com/topic/248089-definitive-way-of-obtaining-the-root-directory/#findComment-1273908 Share on other sites More sharing options...
Muddy_Funster Posted September 29, 2011 Share Posted September 29, 2011 What I have used in the past is getcwd(). It's not the "Absolout" method, but it's more effective cross system. have a look here for some more options: http://php.net/manual/en/book.dir.php Quote Link to comment https://forums.phpfreaks.com/topic/248089-definitive-way-of-obtaining-the-root-directory/#findComment-1273917 Share on other sites More sharing options...
xyph Posted September 29, 2011 Share Posted September 29, 2011 DOCUMENT_ROOT should always be set. If it isn't set, someone didn't set up IIS to work nicely with PHP or you're running PHP through the CLI Here's a solution: $doc_root = str_replace('\\', '/', substr( $_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF']) ) ); From http://php.net/manual/en/reserved.variables.php#63831 Quote Link to comment https://forums.phpfreaks.com/topic/248089-definitive-way-of-obtaining-the-root-directory/#findComment-1273927 Share on other sites More sharing options...
eazyGen Posted September 29, 2011 Author Share Posted September 29, 2011 Thank you both. There is good food for thought here. S Quote Link to comment https://forums.phpfreaks.com/topic/248089-definitive-way-of-obtaining-the-root-directory/#findComment-1273939 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.