Thursday, October 23, 2025
HomeLanguagesPHP dirname( ) Function

PHP dirname( ) Function

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:

  1. 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 (/).
  2. 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/neveropen/gfg.txt")
Output : user01/neveropen

Input : dirname("/neveropen/gfg.txt");
Output : /neveropen

Below programs illustrate the dirname() function:

Program 1:




<?php
  
// specifying path to the dirname() function
echo dirname("user01/neveropen/gfg.txt")
  
?>


Output:

user01/neveropen

Program 2:




<?php
  
// specifying path to the dirname() function
echo dirname("/neveropen/gfg.txt");
  
?>


Output:

/neveropen

Reference:
http://php.net/manual/en/function.dirname.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS