About
In this code snippet, we’ll learn about the dirname() function in PHP.
The dirname() function takes in a path string and gives you the parent directory of it. A second parameter can be added where you can specify how many levels up you want to go.
Let’s see the example code below.
Code:
<?php //__FILE__ contains the path of the current file. $filePath = __FILE__; echo "Current file path: " . $filePath; echo "<br>"; //Gets the path of the parent directory. echo "Parent dir. file path: " . dirname($filePath); echo "<br>"; //You can use the last parameter to specify how many levels up you want to go. echo "Parent of parent dir. file path: " . dirname($filePath, 2); echo "<br>";