PHP getcwd() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Directory

Definition

In PHP, getcwd() returns the current working directory of your script as a string. It answers the question: which folder are relative paths measured from right now?

When PHP resolves a relative path such as 'data.txt', it starts from the current working directory. getcwd() shows you that folder. It is different from __DIR__, which is the folder that contains the script file. getcwd() returns false if the folder cannot be determined.

Syntax

Call getcwd() with the values you need. Start with this line.

PHP
$cwd = getcwd();
CallWhat it does
getcwd()Returns the current working directory, for example /var/www/html. It takes no arguments.
basename(getcwd())Returns just the name of the current folder.
getcwd() . '/data.txt'Builds a full path to a file inside the current folder.

getcwd() and __DIR__ are often the same, but not always. The working directory can change with chdir(), and it can be different when a script is started from another folder on the command line.

Library declarations
PHP
getcwd(): string|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
string|falseReturns the current working directory as a string on success. Returns false on failure. On some Unix systems it can fail if a parent folder has no read or search permission.

PHP Examples

Example 1 — getcwd() Follows chdir()

We start in the script folder, move into a demo folder, and watch getcwd() change while __DIR__ stays the same.

PHP
<?php
chdir(__DIR__);
echo "Start: ", getcwd() === realpath(__DIR__) ? 'same as __DIR__' : 'different', PHP_EOL;

$folder = __DIR__ . '/demo-getcwd';
mkdir($folder);
chdir($folder);

echo "Now in: ", basename(getcwd()), PHP_EOL;
echo "Still same as __DIR__: ", getcwd() === realpath(__DIR__) ? 'yes' : 'no', PHP_EOL;

chdir(__DIR__);
rmdir($folder);

How It Works

  1. chdir(__DIR__) makes sure we start in the script folder, even if the script was launched from somewhere else.
  2. After creating and entering demo-getcwd, getcwd() reports that new folder.
  3. __DIR__ did not change, so comparing the two now gives 'no'.
  4. We go back to __DIR__ before rmdir(), because a folder we are standing in cannot be deleted on Windows.

Example 2 — Checking for false and Relative Paths

getcwd() can return false, so it is wise to check. We also confirm that the relative path '.' means the same folder.

PHP
<?php
$cwd = getcwd();

if ($cwd === false) {
    echo "Could not read the working directory.", PHP_EOL;
} else {
    echo "Got a string: ", is_string($cwd) ? 'yes' : 'no', PHP_EOL;
    echo "'.' is the same folder: ", realpath('.') === $cwd ? 'yes' : 'no', PHP_EOL;
}

How It Works

  1. getcwd() returns false only in rare cases, such as a missing permission on a parent folder.
  2. We test with === false, so an unusual failure does not silently turn into an empty string.
  3. In the normal case we get a string, and is_string() confirms it.
  4. realpath('. ') turns the relative path '.' into a full path, and it matches the result of getcwd().

Example 3 — Building a Path with getcwd()

A relative file name is saved in the working directory. We prove it by building the full path from getcwd().

PHP
<?php
$folder = __DIR__ . '/demo-getcwd-file';
mkdir($folder);
chdir($folder);

file_put_contents('note.txt', 'Hello');
echo "Created: ", basename(getcwd() . DIRECTORY_SEPARATOR . 'note.txt'), PHP_EOL;

$full = getcwd() . DIRECTORY_SEPARATOR . 'note.txt';
echo "Found via getcwd(): ", file_exists($full) ? 'yes' : 'no', PHP_EOL;

chdir(__DIR__);
unlink($folder . '/note.txt');
rmdir($folder);

How It Works

  1. We enter the demo folder, so the working directory is now demo-getcwd-file.
  2. file_put_contents('note. txt', ...) uses a relative path, so the file is created in the working directory.
  3. getcwd() . DIRECTORY_SEPARATOR . 'note.txt' builds the full path using the correct slash for the system.
  4. file_exists() confirms the file is at that exact place, and then we clean up.

Frequently Asked Questions

It returns the current working directory as a string. This is the folder PHP uses to resolve relative paths such as 'data.txt'.
__DIR__ is the folder that contains the file being run, and it never changes. getcwd() is the working directory, which can be changed with chdir() or can depend on how the script was started.
Yes, but it is rare. It can happen on some Unix systems when a parent folder is not readable. Check with === false before using the result.
Normally no, except for the root folder. Add DIRECTORY_SEPARATOR yourself when you join a file name to it.
The working directory depends on how PHP was started. A web server often uses the script's folder or its own folder, while the CLI uses the folder where you typed the command. Use __DIR__ for predictable paths.

Next: opendir()

opendir() opens a folder and returns a directory handle. You then pass that handle to read.

opendir() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful