Re: How can I replace $_SERVER["DOCUMENT_ROOT"] in PHP 7.0?

From: Date: Mon, 13 Sep 2021 21:21:50 +0000
Subject: Re: How can I replace $_SERVER["DOCUMENT_ROOT"] in PHP 7.0?
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-327676@lists.php.net to get a copy of this message
On 13/09/2021 22:09, Bo Berglund wrote:
On Mon, 13 Sep 2021 21:30:18 +0100, Ashley Sheridan <ash@ashleysheridan.co.uk> wrote:
On 13/09/2021 21:17, Bo Berglund wrote:
On Mon, 13 Sep 2021 20:36:35 +0100, Ashley Sheridan <ash@ashleysheridan.co.uk> wrote:
include_once ('appdbdata.php'); THen seemingly all worked, but only until a call was made to a php file at a deeper location which also included this file...
Have you looked at __DIR__ ? https://www.php.net/manual/en/language.constants.magic.php
Yes, I have tested this php file located in the htdocs/ dir: <?php ini_set('display_errors', '1'); set_include_path(get_include_path().PATH_SEPARATOR.dirname($_SERVER['DOCUMENT_ROOT'],1)."/php"); include ('logindata.php'); echo "Including: " .dirname($_SERVER['DOCUMENT_ROOT'],1)."/php/logindata.php"; echo "<br>Mail sender= " . $mailsender ; echo '<br>Dir= ' . __DIR__ ; echo "<br>DocRoot= " . dirname($_SERVER["DOCUMENT_ROOT"],1) . "/"; ?> The output was this: Including: /usr/services/vux/apache/php/logindata.php Mail sender= Dir= /data/25/2/158/91/2484743/user/2721122/htdocs DocRoot= /usr/services/vux/apache/ The strange thing is that even though I have set include_path according to the selected solution in this stackoverflow thread: https://stackoverflow.com/questions/44984560/what-is-the-php7-version-of-serverhome it does not pick up the logindata.php file where $mailsender is defined. If I move the test php file into /php then it picks up the information since it is in the same dir. Note that __DIR__ and dirname($_SERVER['DOCUMENT_ROOT'],1) return completely different paths! It really seems very strange that they have removed this function without supplying a new version...
They might show different paths, it all depends on the main script being run (autoloaders are your friend here). But you can use it to generate paths using some basic string concatenation (and some file methods if you want some better error case handling).
However, my main goal was to be able to use includes across the server so php files in different locations could use the same set of utilities by including them with the correct path. So since this obviously does not work I have to start calculating relative paths from every place I have put the php files, like: include ('../../php/logindata.php'); And what happens if such a file in turn needs an include, is it relative to the original php file called or is it relative to the file which makes the include? __DIR__ will change depending on the included file it's executed from, but so too will paths relative to the current if you construct a path like "./foo/bar.php"
But going back to your original problem, are you sure that $_SERVER['DOCUMENT_ROOT'] doesn't work? I've just tested on a PHP &.2 system, and it does exist and points to the document root of the specific website. -- Ashley Sheridan https://www.ashleysheridan.co.uk

« previous php.general (#327676) next »