PHP dirname( ) Function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The dirname() function in PHP is an inbuilt function which is used to return the directory name of a given path. The dirname() function is used to parent directory's path i.e levels up from the current directory. The dirname() function returns the path of a parent directory which includes a dot ('.') if the path has no slashes, indicating the current directory. Syntax: string dirname ( $path ) Parameters: The dirname() function in PHP accepts only one parameter which is $path. This parameter specifies the path to be checked. Return Value: It returns the path of the parent directory. Errors And Exception: While specifying a path both slashes, forward slash (/) and backslash (\) are used as directory separator character in a windows environment whereas in other environments, it is just the forward slash (/). The dirname() function operates on the input string and therefore it is not aware of the actual filesystem, or path components such as "..". Examples: Input : dirname("user01/geeksforgeeks/gfg.txt") Output : user01/geeksforgeeks Input : dirname("/geeksforgeeks/gfg.txt"); Output : /geeksforgeeks Below programs illustrate the dirname() function: Program 1: php <?php // specifying path to the dirname() function echo dirname("user01/geeksforgeeks/gfg.txt") ?> Output: user01/geeksforgeeks Program 2: php <?php // specifying path to the dirname() function echo dirname("/geeksforgeeks/gfg.txt"); ?> Output: /geeksforgeeks Reference: https://www.php.net/manual/en/function.dirname.php Create Quiz Comment S Shubrodeep Banerjee Follow 0 Improve S Shubrodeep Banerjee Follow 0 Improve Article Tags : Misc Web Technologies PHP PHP-file-handling Explore BasicsPHP Syntax4 min readPHP Variables5 min readPHP | Functions6 min readPHP Loops4 min readArrayPHP Arrays5 min readPHP Associative Arrays4 min readMultidimensional arrays in PHP5 min readSorting Arrays in PHP4 min readOOPs & InterfacesPHP Classes2 min readPHP | Constructors and Destructors5 min readPHP Access Modifiers4 min readMultiple Inheritance in PHP4 min readMySQL DatabasePHP | MySQL Database Introduction4 min readPHP Database connection2 min readPHP | MySQL ( Creating Database )3 min readPHP | MySQL ( Creating Table )3 min readPHP AdvancePHP Superglobals6 min readPHP | Regular Expressions12 min readPHP Form Handling4 min readPHP File Handling4 min readPHP | Uploading File3 min readPHP Cookies9 min readPHP | Sessions7 min read Like