In PHP, what would be the cleanest way to get the parent directory of the current running script relative to the www root? Assume I have:
$_SERVER['SCRIPT_NAME'] == '/relative/path/to/script/index.php'
Or just:
$something_else == '/relative/path/to/script/'
And I need to get /relative/path/to/ with slashes properly inserted. What would you suggest? A one liner is preferred.
EDIT
I need to get a path relative to the www root, dirname(__FILE__) gives me an absolute path in the filesystem so that won’t work. $_SERVER['SCRIPT_NAME'] on the other hand ‘starts’ at the www root.
If your script is located in
/var/www/dir/index.phpthen the following would return:or
Edit
This is a technique used in many frameworks to determine relative paths from the app_root.
File structure:
/var/ www/ index.php subdir/ library.phpindex.php is my dispatcher/boostrap file that all requests are routed to:
library.php is some file located an extra directory down and I need to determine the path relative to the app root (/var/www/).
There’s probably a better way to calculate the relative path then
str_replace()but you get the idea.