PHP dirname() | Get Path of Parent Directory

The PHP dirname() function is used when we need to get the path of the parent directory. For example:

<?php
   echo dirname("C:/xampp/htdocs/index.php");
?>

The output of above PHP example, is:

php dirname function

PHP dirname() Syntax

The syntax of dirname() function in PHP, is:

dirname(path, levels)

The second or levels parameter is optional. The default value of this parameter is 1. The levels parameter is used when we need to specify the number of directories to go up. For example:

<?php
   echo dirname("C:/xampp/htdocs/index.php", 1) . "<br>";
   echo dirname("C:/xampp/htdocs/index.php", 2) . "<br>";
   echo dirname("C:/xampp/htdocs/index.php", 3) . "<br>";
?>

The output of above PHP example, is:

php get path of parent directory

PHP Online Test


« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋