Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14561

Getting the website document root portably (Apache, IIS, maybe others) and simply

From "James Harris" <james.harris.1@gmail.com>
Newsgroups comp.lang.php
Subject Getting the website document root portably (Apache, IIS, maybe others) and simply
Date 2014-11-13 16:28 +0000
Organization A noiseless patient Spider
Message-ID <m42m6j$6vd$1@dont-email.me> (permalink)

Show all headers | View raw


This is a question that has been asked before but there seems no consensus 
in the answers given out there in internet land so I wonder if folks on this 
newsgroup can recommend a currently accepted best-practice solution. What 
should a PHP file do in order to find out the "document root"? I am looking 
for a solution that is portable across different web servers and OSes and is 
simple enough to be present verbatim on any page which needs such info.

As for the DOCUMENT_ROOT value itself:

Page http://www.helicron.net/php-document-root/ says that IIS does not 
include DOCUMENT_ROOT in $_SERVER. The page is dated 2010. Still valid?

https://web.archive.org/web/20120112005237/http://koivi.com/apache-iis-php-server-array.php 
says that DOCUMENT_ROOT is included in IIS. That page is dated 2012; the 
current page it just blank so I had to go to the web archive for it.

http://php.net/manual/en/reserved.variables.server.php links to the CGI/1.1 
specification and says, "There is no guarantee that every web server will 
provide any of these; servers may omit some, or provide others not listed 
here. That said, a large number of these variables are accounted for in 
the ť CGI/1.1 specification, so you should be able to expect those." Even 
with all the caveats DOCUMENT_ROOT is not listed.

Of course, there are lots of other strings that might be combined together 
and otherwise manipulated in order to derive the required information. I 
have tried various potential solutions such as using other $_SERVER vars, 
using realpath(), __FILE__, $_GLOBAL["SCRIPT_NAME"], preg_replace(), etc. I 
can post here if anyone's interested but they all have problems either of 
portability or of complexity. Hence the question: is there a simple, 
portable way to get the web site's base directory in a form that is easy to 
use in subsequent PHP code?

James

Back to comp.lang.php | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Getting the website document root portably (Apache, IIS, maybe others) and simply "James Harris" <james.harris.1@gmail.com> - 2014-11-13 16:28 +0000
  Re: Getting the website document root portably (Apache, IIS, maybe others) and simply "J.O. Aho" <user@example.net> - 2014-11-13 18:38 +0100
    Re: Getting the website document root portably (Apache, IIS, maybe others) and simply Matthew Carter <m@ahungry.com> - 2014-11-13 14:32 -0500
      Re: Getting the website document root portably (Apache, IIS, maybe others) and simply "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 20:58 +0100
      Re: Getting the website document root portably (Apache, IIS, maybe others) and simply "James Harris" <james.harris.1@gmail.com> - 2014-11-13 20:06 +0000
    Re: Getting the website document root portably (Apache, IIS, maybe others) and simply crankypuss <crankypuss@nomail.invalid> - 2014-11-14 02:35 -0700
  Re: Getting the website document root portably (Apache, IIS, maybe others) and simply "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 21:04 +0100
  Re: Getting the website document root portably (Apache, IIS, maybe others) and simply "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2014-11-13 21:53 +0100
  Re: Getting the website document root portably (Apache, IIS, maybe others) and simply Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 22:54 +0100

csiph-web