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.
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 “..”.
Input : dirname("user01/geeksforgeeks/gfg.txt")
Output : user01/geeksforgeeks
Input : dirname("/geeksforgeeks/gfg.txt");
Output : /geeksforgeeks
Below programs illustrate the dirname() function:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!