HOWTO · PHP
How to Get Current Directory Name and Path in PHP
This tutorial introduces how to get the current directory name and path in PHP. Learn various methods including getcwd(), __DIR__, and dirname() to efficiently manage file paths in your PHP applications. Enhance your coding skills and streamline your workflow with these essential techniques.
On this page
PHP has several ways to work with a current path, and the right choice depends on what “current” means. getcwd() returns the process working directory; __DIR__ identifies the directory containing the current PHP file. Those locations can differ when a script is launched from another directory or included by another file. Use dirname() to move from a path to its parent, and basename() when you need only the last component of a path.
Get the Current Working Directory with getcwd()
Call getcwd() to retrieve the process working directory as a path string. This directory is the base for many relative filesystem paths, but it is not necessarily the directory where the script file lives. include and require use additional lookup behavior, including include_path and the calling file’s directory, so getcwd() alone does not describe how every include path is resolved. For example, a command-line process may start in /srv/app and run a script stored in /srv/app/bin; getcwd() refers to /srv/app in that situation. Treat such paths as illustrative because the working directory is determined by how the process was started and may be changed while it runs.
Run this example:
<?php
$workingDirectory = getcwd();
if ($workingDirectory !== false) {
echo $workingDirectory;
}
?>
The example prints the process working directory when PHP can determine it. Its absolute path depends on how the process was started. In application code, check the result of getcwd() before using it: the function can return false if PHP cannot determine the working directory. Handle that result before concatenating paths or passing it to file operations.
Get the Current Script Directory with DIR
The __DIR__ magic constant is the directory of the PHP file in which it appears. Unlike getcwd(), it stays tied to that file when the process starts elsewhere. If bootstrap.php includes lib/config.php, __DIR__ written inside config.php refers to the directory containing config.php, not the directory of bootstrap.php and not necessarily the process working directory.
<?php
$scriptDirectory = __DIR__;
echo $scriptDirectory;
?>
This is useful for files shipped alongside the current script. For example, a script can build a path to a sibling resource from $scriptDirectory, making the lookup independent of the launch directory. The displayed absolute path varies by installation, so this example has no fixed output value. dirname(__FILE__) is an equivalent way to obtain the directory containing the current file; __FILE__ is the full path of that file.
Derive a Directory from a Path with dirname()
Use dirname() when you already have a path and need its parent directory. Applying it to __DIR__ moves one level above the current file’s directory; applying it to __FILE__ returns the directory containing the file. The function operates on the path string and does not verify that the resulting directory exists.
<?php
$parentDirectory = dirname(__DIR__);
echo $parentDirectory;
?>
For a file under /srv/app/src, this pattern derives /srv/app. The sample path is representative; the actual value follows the location of the PHP file. Use the derived path as a base when a resource belongs to a parent folder, and verify filesystem access separately when the operation requires an existing directory.
Get the Directory Name with basename()
basename() returns the final component of the path you supply. With a directory path such as /var/www/project, it returns project; it does not return the full path. With a file path such as /var/www/project/config.php, it returns config.php, which is a filename. The same function can therefore extract a directory name or filename depending on the input path.
Use basename() for display or comparison when only the final component matters. Keep the original full path for file operations: the component alone does not identify a unique location, and it may refer to different directories in different parts of a filesystem.
Choose the Right Path for Your Task
- Use
getcwd()when a relative path should be resolved from the process working directory. Check for itsfalsefailure result before using it. - Use
__DIR__when a path should be based on the PHP file itself, including code loaded throughincludeorrequire. - Use
dirname()to derive a parent directory from__DIR__,__FILE__, or another path. - Use
basename()when you need only the last path component, and retain the full path for locating a resource.
For configuration files, templates, and other resources stored with an application, a path built from __DIR__ is usually more predictable than one based on the process working directory. Choose getcwd() when the caller intentionally controls the working directory, such as a command-line tool operating on a selected folder.