dirname()
The dirname() function in PHP is used to retrieve the directory name from a given path. It's a crucial function for server administrators and web developers who
PHP dirname() Function
The dirname() function returns the parent directory portion of a path. Given a full path to a file or directory, it strips off the trailing component and gives you the directory that contains it. This page covers the syntax, the $levels parameter, edge cases, and how dirname() differs from related path functions.
It's a pure string operation: dirname() parses the text of the path you pass in. It does not touch the filesystem, so the path doesn't have to point at a file that actually exists.
Syntax
dirname(string $path, int $levels = 1): string$path— the path to operate on (required).$levels— how many directory levels to go up. Defaults to1. Available since PHP 7.0; must be1or greater.
It returns the parent directory of $path as a string.
Parameters
| Parameter | Required | Description |
|---|---|---|
$path | Yes | The path string. It does not need to refer to an existing file or directory — dirname() only looks at the text. |
$levels | No | The number of parent levels to climb. dirname($path, 2) is the same as dirname(dirname($path)). Default 1. |
Examples
Get the directory of a file
This retrieves the directory that contains example.txt:
Output:
/home/user1Get the parent of a directory
A trailing slash is treated as the end of a directory name, so dirname() still returns the parent:
Output:
/home/user1Climb multiple levels with $levels
Pass a second argument to go further up the tree in one call:
<?php
echo dirname("/usr/local/lib", 2);Output:
/usrThis is equivalent to dirname(dirname("/usr/local/lib")), but reads more clearly when you need to climb several levels.
Edge cases to know
dirname() has a few results that surprise people the first time:
<?php
echo dirname("example.txt"), "\n"; // "." — no directory part, so current dir
echo dirname("/"), "\n"; // "/" — root has no parent
echo dirname(""), "\n"; // "."- A path with no directory component returns
.(the current directory), not an empty string. - The root path returns itself.
- On Windows, both
/and\are accepted as directory separators.
dirname() vs. related functions
basename()— the opposite ofdirname(): it returns the last component (the file or folder name) instead of the parent directory.pathinfo()— returns an array withdirname,basename,extension, andfilenameall at once, which is handy when you need more than one piece.realpath()— resolves a path against the real filesystem (following symlinks and..), unlikedirname(), which is text-only.
A common pattern is building paths relative to the current file:
<?php
// Load config that sits one directory above this script.
require dirname(__DIR__) . "/config.php";Conclusion
dirname() extracts the parent directory from a path as a simple string operation. Remember that it never checks the filesystem, that a path with no directory returns ., and that the $levels argument lets you climb several directories in a single call. For the complementary file-name half of a path, reach for basename(); for everything at once, use pathinfo().