Get base directory of current script

$_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);

I suggest not to use dirname(). I had several issues with multiple slashes and unexpected results at all. That was the reason why I created currentdir():

function currentdir($url) {
    // note: anything without a scheme ("example.com", "example.com:80/", etc.) is a folder
    // remove query (protection against "?url=http://example.com/")
    if ($first_query = strpos($url, '?')) $url = substr($url, 0, $first_query);
    // remove fragment (protection against "#http://example.com/")
    if ($first_fragment = strpos($url, '#')) $url = substr($url, 0, $first_fragment);
    // folder only
    $last_slash = strrpos($url, '/');
    if (!$last_slash) {
        return '/';
    }
    // add ending slash to "http://example.com"
    if (($first_colon = strpos($url, '://')) !== false && $first_colon + 2 == $last_slash) {
        return $url . '/';
    }
    return substr($url, 0, $last_slash + 1);
}

Why you should not use dirname()

Assume you have image.jpg located in images/ and you have the following code:

<img src="<?php echo $url; ?>../image.jpg" />

Now assume that $url could contain different values:

  • http://example.com/index.php
  • http://example.com/images/
  • http://example.com/images//
  • http://example.com/
  • etc.

Whatever it contains, we need the current directory to produce a working deeplink. You try dirname() and face the following problems:

1.) Different results for files and directories

File
dirname('http://example.com/images/index.php') returns http://example.com/images

Directory
dirname('http://example.com/images/') returns http://example.com

But no problem. We could cover this by a trick:
dirname('http://example.com/images/' . '&') . '/'returns http://example.com/images/

Now dirname() returns in both cases the needed current directory. But we will have other problems:

2.) Some multiple slashes will be removed
dirname('http://example.com//images//index.php') returns http://example.com//images

Of course this URL is not well formed, but multiple slashes happen and we need to act like browsers as webmasters use them to verify their output. And maybe you wonder, but the first three images of the following example are all loaded.

<img src="http://example.com/images//../image.jpg" />
<img src="http://example.com/images//image.jpg" />
<img src="http://example.com/images/image.jpg" />
<img src="http://example.com/images/../image.jpg" />

Thats the reason why you should keep multiple slashes. Because dirname() removes only some multiple slashes I opened a bug ticket.

3.) Root URL does not return root directory
dirname('http://example.com') returns http:
dirname('http://example.com/') returns http:

4.) Root directory returns relative path
dirname('foo/bar') returns .

I would expect /.

5.) Wrong encoded URLs
dirname('foo/bar?url=http://example.com') returns foo/bar?url=http:

All test results:
http://www.programmierer-forum.de/aktuelles-verzeichnis-alternative-zu-dirname-t350590.htm#4329444


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/

Tags:

Php