- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP dirname() | Get Path of Parent Directory
The PHP dirname() function is used when we need to get the path of the parent directory. For example:
<?php echo dirname("C:/xampp/htdocs/index.php"); ?>
The output of the above PHP example is:
PHP dirname() Syntax
The syntax of the dirname() function in PHP is:
dirname(path, levels)
The second, or levels, parameter is optional. The default value of this parameter is 1. The levels parameter is used when we need to specify the number of directories to go up. For example:
<?php echo dirname("C:/xampp/htdocs/index.php", 1) . "<br>"; echo dirname("C:/xampp/htdocs/index.php", 2) . "<br>"; echo dirname("C:/xampp/htdocs/index.php", 3) . "<br>"; ?>
This PHP code outputs the directory names for a given path using the dirname() function with the second argument specifying the number of parent directories to traverse up.
The first line of code invokes the dirname() function, passing it the path "C:/xampp/htdocs/index.php" as the first argument and 1 as the second. This means that the function will return the name of the directory one level up from the file "index.php." The following will be the end result:
C:/xampp/htdocs
The "br" is used to append a line break at the end of the output. Now the second line of code uses the same path as the previous line, but with the second argument set to 2. This means that the function will return the name of the directory two levels up from the file "index.php." The following will be the end result:
C:/xampp
This line of code calls the dirname() function with the same path as the previous lines, but with the second argument set to 3. This means that the function will return the directory name three levels up from the file "index.php". However, since there are only two levels of directories above "index.php", the dirname() function will return the root directory "C:/". The resulting output will be the following:
C:/
Advantages of the dirname() function in PHP
- The dirname() function is portable, as it works on all platforms regardless of operating system or directory separator type.
- The dirname() function normalizes the path to the current operating system, which is useful when working with paths from different operating systems.
- It is a simple and convenient method for obtaining the directory name from a given path or filename.
Disadvantages of the dirname() function in PHP
- The dirname() function only returns the directory name for the next level up in the directory hierarchy. It must be called repeatedly with different levels to gain multiple levels.
- On different operating systems, the dirname() function behaves differently, such as how it handles the directory separator or drive letters, which can result in unexpected results.
- If the provided path or filename is invalid, the dirname() function does not handle errors and may return an unexpected or incorrect value.
« Previous Tutorial Next Tutorial »