getcwd()
Learn how PHP's getcwd() function returns the script's current working directory, when it returns false, and how it differs from __DIR__ and chdir().
The PHP getcwd() Function
getcwd() is a built-in PHP function that returns the current working directory — the directory PHP resolves relative file paths against. It takes no arguments and returns the absolute path as a string, or false on failure.
This page covers the syntax, what the return value really means, the cases where it returns false, and how getcwd() differs from the __DIR__ magic constant and the chdir() function.
Syntax
getcwd(): string|false- Parameters: none.
- Return value: the absolute path of the current working directory on success, or
falseon failure (for example, when a parent directory of the current path has the read or search permission bit unset).
The working directory is process-wide state. It starts as the directory PHP was launched from, not the directory the running script lives in. On a web server it is usually the document root or the server's start directory; on the CLI it is whatever directory you ran the script from.
Basic Example
<?php
echo getcwd();Output (the exact path depends on where the script is run):
/home/user/public_htmlHandling Failure
Because getcwd() can return false, treat the result as string|false rather than assuming a string. This matters when you build paths from it — concatenating false silently produces an empty prefix.
<?php
$dir = getcwd();
if ($dir === false) {
echo "Unable to determine the current working directory.";
} else {
echo "Working in: $dir";
}Building File Paths
A common use is resolving a path relative to wherever the script is being run, then performing file operations there. Always check that the file actually opened before writing to it:
<?php
$current_dir = getcwd();
$file_path = $current_dir . DIRECTORY_SEPARATOR . 'test.txt';
$file_handle = fopen($file_path, 'w');
if ($file_handle === false) {
echo "Could not open file for writing.";
} else {
fwrite($file_handle, 'This is a test file.');
fclose($file_handle);
echo "File written to: $file_path";
}Using the DIRECTORY_SEPARATOR constant instead of a hard-coded / keeps the path correct across operating systems. See fopen() for the available file modes.
getcwd() vs. DIR
This is the most common source of bugs. getcwd() returns the runtime working directory, which can change. __DIR__ is resolved at compile time and always points to the directory of the current source file — it never changes, even after a call to chdir().
- Use
__DIR__to include files or load assets that live next to your script (require __DIR__ . '/config.php';). This is almost always what you want for project-relative paths. - Use
getcwd()when you genuinely care about where the process was started, such as a CLI tool that operates on the user's current folder.
The working directory can be moved with chdir():
<?php
echo getcwd(), PHP_EOL; // e.g. /home/user/project
chdir('..');
echo getcwd(), PHP_EOL; // parent directory, e.g. /home/userRelated Functions
chdir()— change the current working directory.realpath()— expand a relative path to a canonical absolute path.dirname()— get the parent directory portion of a path.scandir()— list the entries inside a directory.
Conclusion
getcwd() returns the absolute path of the process's current working directory, or false if it can't be read. Check for false before using the result, prefer DIRECTORY_SEPARATOR when joining paths, and reach for __DIR__ instead when you need a path relative to the script file rather than the runtime directory.