The dirname Function
The dirname function returns the path to the parent directory of the specified path. The first parameter is a string with the path, and the second optional parameter is the number of levels to go up.
Syntax
phpdirname(string $path, int $levels = 1): string
Example
Get the parent directory for the path:
php<?php
echo dirname('/var/www/html/index.php');
?>
Code execution result:
php'/var/www/html'
Example
Go up two directory levels:
php<?php
echo dirname('/var/www/html/index.php', 2);
?>
Code execution result:
php'/var'
Example
Working with relative paths:
php<?php
echo dirname('images/photo.jpg');
?>
Code execution result:
php'images'