Re: How can I replace $_SERVER["DOCUMENT_ROOT"] in PHP 7.0?
[email protected] (Ashley Sheridan)
| Newsgroups | php.general |
|---|---|
| Message-ID | <[email protected]> |
On 13/09/2021 21:17, Bo Berglund wrote: > On Mon, 13 Sep 2021 20:36:35 +0100, Ashley Sheridan <[email protected]> > 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). -- Ashley Sheridan https://www.ashleysheridan.co.uk