W3docs

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 to 1. Available since PHP 7.0; must be 1 or greater.

It returns the parent directory of $path as a string.

Parameters

ParameterRequiredDescription
$pathYesThe path string. It does not need to refer to an existing file or directory — dirname() only looks at the text.
$levelsNoThe 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:

php— editable, runs on the server

Output:

/home/user1

Get the parent of a directory

A trailing slash is treated as the end of a directory name, so dirname() still returns the parent:

php— editable, runs on the server

Output:

/home/user1

Climb 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:

/usr

This 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.
  • basename() — the opposite of dirname(): it returns the last component (the file or folder name) instead of the parent directory.
  • pathinfo() — returns an array with dirname, basename, extension, and filename all at once, which is handy when you need more than one piece.
  • realpath() — resolves a path against the real filesystem (following symlinks and ..), unlike dirname(), 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().

Practice

Practice
What is the purpose of the dirname() function in PHP?
What is the purpose of the dirname() function in PHP?
Was this page helpful?