HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

dirname

Description

The dirname of Filesystem for PHP 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

<?

$path = "/usr/local/lib";

echo dirname($path, 1) . PHP_EOL;
echo dirname($path, 2) . PHP_EOL;
echo dirname($path, 3);
/usr/local
/usr
/