PHP dirname() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, dirname() returns the folder part of a path, which is everything before the last slash. For /var/www/index.php it returns /var/www.

You can pass a number as the second argument to go up more than one folder. Like basename(), it only works on the text of the path and does not check if the folder exists.

Syntax

Call dirname() with the values you need. Start with this line.

PHP
$folder = dirname("/var/www/html/index.php");
CallWhat it does
dirname("/var/www/html/index.php")Returns /var/www/html, the folder of the file.
dirname("/var/www/html/index.php", 2)Goes up two levels and returns /var/www.
dirname(__FILE__)Returns the folder of the current script, the same as __DIR__.

The second argument was added in PHP 7.0. A path without a slash such as file.txt returns a single dot, which means the current folder.

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

Parameters

ParameterDescription
pathRequired. The path as a string.
levelsOptional. How many folders to go up. It must be 1 or more, and the default is 1.

Return Value

Return ValueDescription
stringThe path of the parent folder. It returns . if the path has no folder part, and / for the root.

PHP Examples

Example 1 — Get the Folder of a File

Return the parent folder, and then go up two levels.

PHP
<?php
$path = "/var/www/html/index.php";

echo dirname($path), PHP_EOL;
echo dirname($path, 2), PHP_EOL;

How It Works

  1. We store a path with a file name at the end.
  2. dirname($path) removes the file name and returns the folder.
  3. dirname($path, 2) goes up one more level.
  4. Each result is printed on its own line.

Example 2 — Paths Without a Folder

See what dirname() returns for a bare file name, the root, and a path with a trailing slash.

PHP
<?php
echo dirname("file.txt"), PHP_EOL;
echo dirname("/"), PHP_EOL;
echo dirname("/var/www/"), PHP_EOL;
echo dirname("/var"), PHP_EOL;

How It Works

  1. file. txt has no folder, so the result is a dot for the current folder.
  2. The root path stays as /.
  3. A trailing slash is ignored, so /var/www/ gives /var.
  4. /var has the root as its parent, which is /.

Example 3 — Build a Path Next to the Current Script

dirname(__FILE__) gives the same folder as __DIR__. Use it to build paths for other files.

PHP
<?php
var_dump(dirname(__FILE__) === __DIR__);

$config = dirname(__DIR__) . "/config.php";

echo basename($config), PHP_EOL;

How It Works

  1. dirname(__FILE__) and __DIR__ both give the folder of this script.
  2. var_dump() shows that they are equal.
  3. dirname(__DIR__) goes one folder above the script folder.
  4. We add config. php to that folder and print its file name with basename().

Frequently Asked Questions

It returns the folder part of a path, which is everything before the last slash. It does not check the disk.
Pass 2 as the second argument, like dirname($path, 2). This needs PHP 7.0 or newer.
A path with no slash, such as file.txt, has no folder part, so PHP returns . to mean the current folder.
They give the same result. __DIR__ is shorter and was added in PHP 5.3.
dirname() returns the folder, while basename() returns the last part, which is usually the file name.

Next: disk_free_space()

disk_free_space() returns how many bytes are free on the disk or partition that contains a.

disk_free_space() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful