dirname
Returns the path of the folder that holds a file.
Syntax
dirname(string $path, int $levels = 1): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $path | string | The path. |
| $levels | int | Optional. 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/siteNotes
The project root from an entry point in the public folder is obtained with dirname(__DIR__).
See also
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.