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 // specifying path to the dirname() function echo dirname("user01/geeksforgeeks/gfg.txt") ?> |
Output:
user01/geeksforgeeks
Program 2:
<?php // specifying path to the dirname() function echo dirname("/geeksforgeeks/gfg.txt"); ?> |
Output:
/geeksforgeeks
Reference:
http://php.net/manual/en/function.dirname.php
Recommended Posts:
- HTML | dirname Attribute
- HTML | <input> dirname Attribute
- HTML | <textarea> dirname Attribute
- HTML | <textarea> dirname Attribute
- Node.js | path.dirname() Method
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

