How to Get Current Directory Name and Path in PHP
-
Use the
getcwd()
Function to Get the Current Directory Name in PHP -
Use the
dirname()
Function to Get the Current Directory Name in PHP -
Use the
basename()
Function to Get the Current Directory Name in PHP
This article will introduce a few methods to get the current working directory name in PHP.
Use the getcwd()
Function to Get the Current Directory Name in PHP
The getcwd()
function gives the current working directory. The returned value is a string on success.
The function does not take any parameters. The function returns false in case of failure.
Let’s consider the following directory structure.
├── var
│ └── www
│ └── html
| └──project
| └──index.php
The PHP file lies inside the project
directory. The getcwd()
function will return the name of the current working directory, which is project
.
We can use the echo
function to display the content of the function. We can see in the output section that the getcwd()
function returns the current working directory with its path.
echo getcwd();
Output:
/var/www/html/project
Use the dirname()
Function to Get the Current Directory Name in PHP
We can also use the dirname()
function to get the current directory name in PHP. The function returns the path of the parent directory.
It accepts two parameters where the first one is the path and the second one is levels. Levels indicate the number of directories to move up.
Finally, we can use the __FILE__
magic constants in the dirname()
function to get the name of the current directory. The __FILE__
constant returns the full path of the current file along with the file name.
We can demonstrate these constants and the function in the above directory structure. For example, we get the following result when we echo the __FILE__
constant from the index.php
file.
echo __FILE__;
Output:
/var/www/html/project/index.php
For example, write the dirname()
function in the index.php
file with the __FILE__
constant as the parameter.
echo dirname(__FILE__);
Output:
/var/www/html/project
In this way, we can get the current working directory name in PHP.
Use the basename()
Function to Get the Current Directory Name in PHP
We can use the basename()
function to get the current working directory name without the path in PHP. We can apply this function with the result of the above two functions.
The basename()
function returns the name of the base file or folder from the given path. For example, if the path provided is /var/www/html/project
, the output will be project
.
For example, use the functions dirname(__FILE__)
and getcwd()
as the parameters for the basename()
function. In this way, we can get the current working directory name in PHP.
echo basename(dirname(__FILE__))."<br>";
echo basename(getcwd())."\n";
Output:
project
project
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedInRelated Article - PHP File
- How to Write Into a File in PHP
- How to Require_once vs Include in PHP
- How to Create and Get the Path of tmpfile in PHP
- How to Remove PHP Extension With .Htacess File
- How to Move File to a Folder in PHP
- How to Set End of Line in PHP With PHP_EOL