Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

dirname

Returns the path of the folder that holds a file.

Syntax

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

Parameters

ParameterTypeDescription
$pathstringThe path.
$levelsintOptional. How many levels to go up.

Return value

stringThe path without its last part.

Examples

A file's folder and the folder above:

<?php
 
echo dirname('/var/www/site/public/index.php'), "\n";
echo dirname('/var/www/site/public/index.php', 2);

Output:

/var/www/site/public
/var/www/site

Notes

The project root from an entry point in the public folder is obtained with dirname(__DIR__).

See also

  • basename — Returns the last part of a path — the file name.
  • pathinfo — Splits a path into parts: the folder, the name and the extension.

In the tutorial

  • Folders and paths — Working with folders in PHP — creating them, listing files, searching by a mask, parts of a path and file information.

Thanks for reading!

Donate to the site — we work hard for you.