note 59245 deleted from reserved.variables by cmb
| From: | cmb@php.net | Date: | Tue, 05 Nov 2019 11:33:34 +0000 |
| Subject: | note 59245 deleted from reserved.variables by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212912@lists.php.net to get a copy of this message | ||
Note Submitter: chris at vault5 dot com
----
Since $_SERVER['DOCUMENT_ROOT'] is not always present, the following will provide it where
$_SERVER dosen't.
<?php
function resolveDocumentRoot() {
$current_script = dirname($_SERVER['SCRIPT_NAME']);
$current_path = dirname($_SERVER['SCRIPT_FILENAME']);
/* work out how many folders we are away from document_root
by working out how many folders deep we are from the url.
this isn't fool proof */
$adjust = explode("/", $current_script);
$adjust = count($adjust)-1;
/* move up the path with ../ */
$traverse = str_repeat("../", $adjust);
$adjusted_path = sprintf("%s/%s", $current_path, $traverse);
/* real path expands the ../'s to the correct folder names */
return realpath($adjusted_path);
}
?>
It counts the number of folders down the path we are in the URL, then moves that number of folders
up the current path... end result should be the document root :)
It wont work with virtual folders or in any situation where the folder in the URL dosen't map
to a real folder on the disk (like when using rewrites).