chdir()
Changing the working directory in PHP is a common task that many developers need to perform. In this article, we will discuss how to change the directory using
The PHP chdir() function changes the current working directory of your script to the directory you pass to it. This page explains what the working directory is, the exact signature of chdir(), how to handle the value it returns, and the common cases and gotchas you will hit in real code.
What is the working directory?
The current working directory (CWD) is the base location PHP uses to resolve relative paths. When you call something like fopen('data.txt', 'r') or include 'config.php', PHP joins that relative path onto the CWD to figure out which file you mean.
A common misconception is that the CWD is always the folder containing the running script. That is only the starting point in some setups. The CWD can differ from the script's directory — for example when a script is launched by cron, by a web server, or from another working directory on the command line. When the path matters, never assume; read it back with getcwd().
Syntax
chdir(string $directory): bool$directory— the path to switch to. It may be absolute (/var/www/html) or relative to the current CWD (../logs).- Return value —
trueon success,falseon failure (for example, if the directory does not exist or the process lacks permission).chdir()emits anE_WARNINGon failure, so you should check the return value rather than ignore it.
Basic usage
<?php
// Where are we now?
echo getcwd() . PHP_EOL; // e.g. /var/www/html
// Move into a subdirectory
chdir('logs');
echo getcwd() . PHP_EOL; // e.g. /var/www/html/logs
// Move back up one level
chdir('..');
echo getcwd() . PHP_EOL; // e.g. /var/www/htmlBecause chdir() accepts relative paths, chdir('logs') is resolved against wherever you currently are, while chdir('..') walks one directory up.
Always check the return value
A failed chdir() leaves the CWD unchanged, which can silently break every relative path that follows it. Guard the call:
<?php
$target = '/path/that/may/not/exist';
if (chdir($target)) {
echo "Now working in: " . getcwd() . PHP_EOL;
} else {
echo "Could not change to {$target}" . PHP_EOL;
}Save and restore the original directory
Changing the CWD affects the whole process, not just the current function. If a helper switches directories and forgets to switch back, later code may read or write the wrong files. A safe pattern is to capture the original directory and restore it when you are done:
<?php
$original = getcwd(); // remember where we started
chdir('/tmp');
// ... do work that relies on /tmp being the CWD ...
chdir($original); // restore for the rest of the script
echo getcwd() . PHP_EOL; // back to where we startedCombining chdir() with includes
Once the CWD is changed, relative include/require paths resolve from the new location:
<?php
chdir('/path/to/app/config');
include 'database.php'; // resolves to /path/to/app/config/database.phpFor includes specifically, relying on the CWD is fragile because callers can change it. Prefer an absolute path built from the script's own location with the magic constant __DIR__:
<?php
// Robust regardless of the current working directory
include __DIR__ . '/config/database.php';When would I use chdir()?
- CLI scripts and build tools that shell out to commands expecting a specific directory.
- Batch jobs (run via cron) where the launching CWD is unpredictable, so you set it explicitly first.
- Working with relative paths in bulk — e.g. iterating files in one folder without prefixing every path.
For most web applications you should prefer absolute paths (__DIR__, configured base paths) over changing the global CWD, since the change leaks across the entire request.
Related functions
getcwd()— read the current working directory.mkdir()— create a directory before switching into it.scandir()/opendir()— list the contents of a directory.realpath()— expand a relative path to its absolute, symlink-resolved form.dirname()— get the directory portion of a path string.