Snoob Snoob - 6 months ago 16
PHP Question

Get base directory of current script

This is the url of my script:

localhost/do/index.php


I want a variable or a function that returns
localhost/do
(something like
$_SERVER['SERVER_NAME'].'/do'
)

Answer

Try:

$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$url);
print_r($parts);

EDIT:

$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$url);
$dir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++) {
 $dir .= $parts[$i] . "/";
}
echo $dir;

This should return localhost/do/