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

[email protected] (Bo Berglund)
Newsgroups php.general
Message-ID <[email protected]>
On Tue, 14 Sep 2021 14:48:57 +0100, AllenJB <[email protected]> wrote:

>To account for symlinks (which allow you to pretend a file or directory 
>is in another place on the filesystem), run each path through realpath() 
>(I believe __DIR__ is already resolved for its "real path", but it 
>doesn't hurt to run it through again in this case to be sure)

I used this php file put in a subdir below document root:

<?php
$rootdir= substr(__DIR__, 0, strpos( __DIR__, 'htdocs')) . 'htdocs';
echo "Root dir: " . $rootdir . "<br>";
echo "Real root path: " . realpath($rootdir) . "<br>";
$docroot=$_SERVER["DOCUMENT_ROOT"];
echo "Document root: " . $docroot . "<br>";
echo "Real document root: " . realpath($docroot) . "<br>";
include ($rootdir . '/php/logindata.php');
echo "Mail sender using Real root path: " . $mailsender ;
?>

The printed output whan I call this php file with FireFox:

Root dir: /data/25/2/158/91/2484743/user/2721122/htdocs
Real root path: /data/25/2/158/91/2484743/user/2721122/htdocs
Document root: /usr/services/vux/apache/htdocs
Real document root: /usr/services/vux/apache/htdocs
Mail sender using Real root path: [email protected]

Notice that the two paths grabbed by __DIR__ and $_SERVER["DOCUMENT_ROOT"] are
clearly different and using the latter in the include will make the $mailsender
variable empty.


-- 
Bo Berglund
Developer in Sweden
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.