Another solution, without dirname and respecting http and https is to use the old fashioned get path method (source from this stackoverflow answer).
Then check if the path ends with the fileextension ".php". If true, remove it from the path.
Long version
$current_url_path = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (substr($current_url_path, -4) == ".php") {
    // path ends with .php
    // split string to array
    $arr = explode("/", $current_url_path);
    // remove last array item (after the last / which is the file)
    $arrS = array_slice($arr, 0, -1);
    // join the array to a string
    $current_url_path = implode("/", $arrS);
}
echo $current_url_path;
Shortened version
$current_url_path = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$current_url_path = substr($current_url_path, -4) == ".php" ?
    implode("/", array_slice((explode("/", $current_url_path)), 0, -1)) : $current_url_path;
echo $current_url_path;
Test
This url: http://localhost/comp-test/test.php is returned as http://localhost/comp-test