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


Groups > comp.lang.php > #14561 > unrolled thread

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

Started by"James Harris" <james.harris.1@gmail.com>
First post2014-11-13 16:28 +0000
Last post2014-11-13 22:54 +0100
Articles 9 — 7 participants

Back to article view | Back to comp.lang.php


Contents

  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

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

From"James Harris" <james.harris.1@gmail.com>
Date2014-11-13 16:28 +0000
SubjectGetting the website document root portably (Apache, IIS, maybe others) and simply
Message-ID<m42m6j$6vd$1@dont-email.me>
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

[toc] | [next] | [standalone]


#14562

From"J.O. Aho" <user@example.net>
Date2014-11-13 18:38 +0100
Message-ID<cck8l8F3b8nU1@mid.individual.net>
In reply to#14561
On 13/11/14 17:28, James Harris wrote:
> 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.

This depends on the webserver used, which method of how PHP is used 
(mod_php, php-fpm, fastcgi and so on) togehter with the webserver and 
even if you have the DOCUMENT_ROOT, it may not be pointing on the 
directory you think it may be.

You will simply have to figure it out for each webserver and how the php 
is used and hope that the configuration will not be using rewrites to 
lessen the restart of the webservise when hosting a large number of 
sites (then you will have a DOCUMENT_ROOT which includes all the users 
home directories) and then make cases for those options and from phpinfo 
determine which method needs to be used.


-- 

  //Aho

[toc] | [prev] | [next] | [standalone]


#14563

FromMatthew Carter <m@ahungry.com>
Date2014-11-13 14:32 -0500
Message-ID<87ppcq3nh8.fsf@ahungry.com>
In reply to#14562
"J.O. Aho" <user@example.net> writes:

> On 13/11/14 17:28, James Harris wrote:
>> 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.
>
> This depends on the webserver used, which method of how PHP is used
> (mod_php, php-fpm, fastcgi and so on) togehter with the webserver and
> even if you have the DOCUMENT_ROOT, it may not be pointing on the
> directory you think it may be.
>
> You will simply have to figure it out for each webserver and how the
> php is used and hope that the configuration will not be using rewrites
> to lessen the restart of the webservise when hosting a large number of
> sites (then you will have a DOCUMENT_ROOT which includes all the users
> home directories) and then make cases for those options and from
> phpinfo determine which method needs to be used.

How about:

$doc_root = substr (__FILE__, 0,
                    strpos (__FILE__, $_SERVER['SCRIPT_NAME']));

SCRIPT_NAME falls into the CGI spec so should be there unless you're
using some partial implementation web server, and __FILE__ is common
even on EOL PHP implementations.

-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

[toc] | [prev] | [next] | [standalone]


#14564

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-11-13 20:58 +0100
Message-ID<m432g1$kbg$1@solani.org>
In reply to#14563
Matthew Carter wrote:

> "J.O. Aho" <user@example.net> writes:
> 
>> On 13/11/14 17:28, James Harris wrote:
>>> 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.
>>
>> This depends on the webserver used, which method of how PHP is used
>> (mod_php, php-fpm, fastcgi and so on) togehter with the webserver and
>> even if you have the DOCUMENT_ROOT, it may not be pointing on the
>> directory you think it may be.
>>
>> You will simply have to figure it out for each webserver and how the
>> php is used and hope that the configuration will not be using rewrites
>> to lessen the restart of the webservise when hosting a large number of
>> sites (then you will have a DOCUMENT_ROOT which includes all the users
>> home directories) and then make cases for those options and from
>> phpinfo determine which method needs to be used.
> 
> How about:
> 
> $doc_root = substr (__FILE__, 0,
>                     strpos (__FILE__, $_SERVER['SCRIPT_NAME']));
> 
> SCRIPT_NAME falls into the CGI spec so should be there unless you're
> using some partial implementation web server, and __FILE__ is common
> even on EOL PHP implementations.

From the CGI specification[1]:

| The SCRIPT_NAME variable MUST be set to a URI path [...]
                                            ^^^^^^^^

So your suggestion does not necessarily work.

[1] <http://tools.ietf.org/html/rfc3875#section-4.1.13>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14566

From"James Harris" <james.harris.1@gmail.com>
Date2014-11-13 20:06 +0000
Message-ID<m432vc$slj$1@dont-email.me>
In reply to#14563
"Matthew Carter" <m@ahungry.com> wrote in message 
news:87ppcq3nh8.fsf@ahungry.com...
> "J.O. Aho" <user@example.net> writes:
>
>> On 13/11/14 17:28, James Harris wrote:
>>> 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.
>>
>> This depends on the webserver used, which method of how PHP is used
>> (mod_php, php-fpm, fastcgi and so on) togehter with the webserver and
>> even if you have the DOCUMENT_ROOT, it may not be pointing on the
>> directory you think it may be.
>>
>> You will simply have to figure it out for each webserver and how the
>> php is used and hope that the configuration will not be using rewrites
>> to lessen the restart of the webservise when hosting a large number of
>> sites (then you will have a DOCUMENT_ROOT which includes all the users
>> home directories) and then make cases for those options and from
>> phpinfo determine which method needs to be used.
>
> How about:
>
> $doc_root = substr (__FILE__, 0,
>                    strpos (__FILE__, $_SERVER['SCRIPT_NAME']));
>
> SCRIPT_NAME falls into the CGI spec so should be there unless you're
> using some partial implementation web server, and __FILE__ is common
> even on EOL PHP implementations.

I tried something using those two values and thought it had worked until I 
included one file in another. Unfortunately, __FILE__ is the name of the 
current file whereas the 'script name' is that of the file which originally 
did the including. So if you have A.php which includes B.php which includes 
C.php __FILE__ will be set to each name in turn as that file is parsed but 
the script name will always be A.php. I think that would make such code hard 
to place.

I had a similar thought about always using a two-level structure with, say,

  /main/*.php
  /utils/*.php

and having each include written as "../utils/X.php" etc. That works for all 
pages which are one level deep but not for the home page which is one level 
above them. It seems that if that home page includes, say, "utils/X.php" 
then if X.php tries to include another file it will try to include it be 
relative to the webroot rather than relative to webroot/utils.

James

[toc] | [prev] | [next] | [standalone]


#14580

Fromcrankypuss <crankypuss@nomail.invalid>
Date2014-11-14 02:35 -0700
Message-ID<m44icm$7td$1@dont-email.me>
In reply to#14562
On 11/13/2014 10:38 AM, J.O. Aho wrote:
> On 13/11/14 17:28, James Harris wrote:
>> 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.
>
> This depends on the webserver used, which method of how PHP is used
> (mod_php, php-fpm, fastcgi and so on) togehter with the webserver and
> even if you have the DOCUMENT_ROOT, it may not be pointing on the
> directory you think it may be.
>
> You will simply have to figure it out for each webserver and how the php
> is used and hope that the configuration will not be using rewrites to
> lessen the restart of the webservise when hosting a large number of
> sites (then you will have a DOCUMENT_ROOT which includes all the users
> home directories) and then make cases for those options and from phpinfo
> determine which method needs to be used.
>
>

Do note that in some cases (eg godaddy) in addition to "DOCUMENT_ROOT" 
being set, "SUBDOMAIN_DOCUMENT_ROOT" may also be set.

In every case I have seen, one can count on the __FILE__ being set to 
the currently executing file, though that is mostly useful in an 
index.php situation since otherwise __FILE__ might be in some include 
directory or something else that confuses the issue.

And of course I haven't worked with every permutation of invoking 
methodology, so what I'd suggest is [a] find something that works for 
your situation, [b] make it as general as possible without expecting the 
moon, [c] when (not if) you get confused, do something else for a while.

[toc] | [prev] | [next] | [standalone]


#14565

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-11-13 21:04 +0100
Message-ID<m432rb$llt$1@solani.org>
In reply to#14561
James Harris wrote:

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

If the PHP script is placed in the webroot you can use:

  realpath('.')

If the script is placed somewhere else use the respective relative path
to the webroot as argument.  In most cases you don't need an absolute
path, though.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14569

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2014-11-13 21:53 +0100
Message-ID<cckk2rF6dq5U1@mid.uni-berlin.de>
In reply to#14561
On 13.11.2014 17:28, James Harris wrote:
> 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.
>

My 2 ¢

In a code base serving several virtual hosts from the same location (apache2 
ServerAlias, or completely separate) I started putting the document root in a db 
config table, so I would have an easy way to combine or separate document roots 
depending on server names (HTTP_HOST).

Together with a config file named after _SERVER[HTTP_HOST] this gave me (and the app) 
flexibility and stability.

/Str.

[toc] | [prev] | [next] | [standalone]


#14574

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-11-13 22:54 +0100
Message-ID<2398525.OyAPUs1pPJ@PointedEars.de>
In reply to#14561
James Harris wrote:

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

Short answer: Do not rely on DOCUMENT_ROOT.

> What should a PHP file do in order to find out the "document root"?

Here is a better question: Why should "a PHP file" do that in the first 
place?

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

Reads like a solution desperately looking for a problem.

<http://www.catb.org/~esr/faqs/smart-questions.html#goal>
 
> 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?

I do not know (fortunately, I do not have to deal with IIS since 2 years 
anymore).  If you need to support it, install IIS & friends and try it out.
 
> 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.

Why not go to <http://msdn.microsoft.com/> instead?  Or read the source code 
for the PHP ISAPI module.

That said, PHP with FastCGI is recommended over ISAPI nowadays, partially 
because PHP 5.4 and above do not come with an ISAPI module anymore.

You could have found that out by going to <http://php.net/> and RTFM instead 
of unearthing dead Web sites:

<http://php.net/manual/en/migration53.windows.php>
<http://php.net/manual/en/install.windows.php>
 
> […] 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?

No.

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web