PHPerKaigi 2019


Please answer this simple SPAM challenge: max(nine, three)?
(Example: nine)

The Note You're Voting On

chris at vault5 dot com
13 years ago
Since $_SERVER['DOCUMENT_ROOT'] is not always present, the following will provide it where $_SERVER dosen't.

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

<< Back to user notes page

To Top