dirname

Returns a parent directory's path

Syntax

dirname(string $path, int $levels = 1): string

Parameters

path

A path.

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).

levels

The number of parent directories to go up.

This must be an integer greater than 0.

Return

Returns the path of a parent directory. If there are no slashes in path, a dot ('.') is returned, indicating the current directory. Otherwise, the returned string is path with any trailing /component removed.

Examples

1 · path

<?

echo dirname("/etc/passwd?a=1") . PHP_EOL;
echo dirname("/etc/passwd") . PHP_EOL;
echo dirname("/etc/?a=1") . PHP_EOL;
echo dirname("/etc/") . PHP_EOL;
echo dirname(".") . PHP_EOL;
echo dirname("C:\\");

?>
/etc
/etc
/etc
/
.
.

2 · levels

<?

echo dirname("/usr/local/lib", 1) . PHP_EOL;
echo dirname("/usr/local/lib", 2);

?>
/usr/local
/usr
HomeMenu