PHP chdir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Directory

Definition

In PHP, chdir() changes the current working directory to a folder you choose. After that, relative paths like 'data.txt' are looked up inside the new folder.

Every running PHP script has a current working directory, which is the folder PHP uses when you write a relative path. chdir() moves that starting point. It returns true on success and false (plus a warning) if the folder does not exist or cannot be entered.

Syntax

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

PHP
chdir(__DIR__ . '/uploads');
CallWhat it does
chdir(__DIR__ . '/uploads')Moves into the uploads folder that sits next to the script.
chdir('..')Moves up one level to the parent folder.
chdir(__DIR__)Moves back to the folder that holds the current script.

chdir() only changes where relative paths start. It does not change __DIR__, which always points to the folder of the file being run. Use getcwd() to see where you are now.

Library declarations
PHP
chdir(string $directory): bool

Parameters

ParameterDescription
directoryRequired. The path of the folder to move into. It can be absolute, like /var/www/data, or relative, like .. or uploads.

Return Value

Return ValueDescription
boolReturns true if the working directory was changed. Returns false on failure, for example when the folder does not exist, and PHP also raises an E_WARNING.

PHP Examples

Example 1 — Moving Into a Folder and Using a Relative Path

We create a small demo folder with one file, move into it with chdir(), and then open the file using only its name.

PHP
<?php
$folder = __DIR__ . '/demo-chdir';
mkdir($folder);
file_put_contents($folder . '/hello.txt', 'Hi');

chdir($folder);
echo "Current folder: ", basename(getcwd()), PHP_EOL;
echo "File found: ", file_exists('hello.txt') ? 'yes' : 'no', PHP_EOL;
echo "Content: ", file_get_contents('hello.txt'), PHP_EOL;

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

How It Works

  1. mkdir() creates the demo folder and file_put_contents() writes hello. txt inside it.
  2. chdir($folder) moves the working directory into demo-chdir.
  3. Now the short, relative name 'hello. txt' works, because PHP looks inside the current folder.
  4. We move back with chdir(__DIR__) and then delete the file and folder, so the demo leaves nothing behind.

Example 2 — What Happens When the Folder Does Not Exist

chdir() returns false when it cannot move. The working directory stays exactly where it was.

PHP
<?php
$before = getcwd();

$result = @chdir(__DIR__ . '/no-such-folder');
var_dump($result);

echo "Still in same folder: ", getcwd() === $before ? 'yes' : 'no', PHP_EOL;

How It Works

  1. We save the current folder in $before so we can compare later.
  2. The folder 'no-such-folder' does not exist, so chdir() fails and returns false.
  3. The @ symbol hides the warning that PHP would normally print, which keeps this demo output clean.
  4. Since the move failed, getcwd() still returns the same folder as before.

Example 3 — Going Up with '..'

Relative paths work with chdir() too. Here we go down into a nested folder and then back up using '..'.

PHP
<?php
$root = __DIR__ . '/demo-chdir-tree';
mkdir($root . '/inner', 0777, true);

chdir($root . '/inner');
echo basename(getcwd()), PHP_EOL;

chdir('..');
echo basename(getcwd()), PHP_EOL;

chdir(__DIR__);
rmdir($root . '/inner');
rmdir($root);

How It Works

  1. mkdir() with true as the third argument creates the outer and inner folders together.
  2. chdir() moves into inner, and basename(getcwd()) prints just the folder name.
  3. chdir('. .') moves up one level, back into demo-chdir-tree.
  4. We return to __DIR__ first, because a folder cannot be removed while the script is standing inside it.

Frequently Asked Questions

It changes the current working directory of your script. After the change, any relative file path, such as 'data.txt' or 'images/logo.png', is resolved starting from the new folder.
No. __DIR__ is fixed to the folder of the file that contains it. chdir() only affects the working directory, which you can read with getcwd().
It returns false and raises an E_WARNING. The working directory does not change. Check the result, or test with is_dir() first, before relying on the new folder.
On Windows, a folder that is in use as the working directory is locked. Move out with chdir(__DIR__) or chdir('..') first, and then call rmdir().
Absolute paths built with __DIR__ are usually safer because they do not depend on a hidden global setting. Use chdir() when a script or library really needs relative paths to start from a certain folder.

Next: chroot()

chroot() changes the process root directory to a folder you choose. After that, the script.

chroot() 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