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 Comment S Shubrodeep Banerjee Follow 0 Improve S Shubrodeep Banerjee Follow 0 Improve Article Tags : Misc Web Technologies PHP PHP-file-handling Explore PHP Tutorial 8 min read BasicsPHP Syntax 4 min read PHP Variables 5 min read PHP | Functions 8 min read PHP Loops 4 min read ArrayPHP Arrays 5 min read PHP Associative Arrays 4 min read Multidimensional arrays in PHP 5 min read Sorting Arrays in PHP 4 min read OOPs & InterfacesPHP Classes 2 min read PHP | Constructors and Destructors 5 min read PHP Access Modifiers 4 min read Multiple Inheritance in PHP 4 min read MySQL DatabasePHP | MySQL Database Introduction 4 min read PHP Database connection 2 min read PHP | MySQL ( Creating Database ) 3 min read PHP | MySQL ( Creating Table ) 3 min read PHP AdvancePHP Superglobals 6 min read PHP | Regular Expressions 12 min read PHP Form Handling 4 min read PHP File Handling 4 min read PHP | Uploading File 3 min read PHP Cookies 9 min read PHP | Sessions 7 min read Like