PHP chdir() Directory

PHP chdir() Directory

In this tutorial, we will explore how we can use PHP to change between directories within a filesystem.

If you have used a terminal before, whether Windows or Linux, you are probably familiar with the cd command.

This command allows us to change directory to any location within the filesystem. This enables us to quickly and efficiently switch to various locations in the filesystem without the need for a Graphical Interface.

PHP chdir() Function

The chdir() function in PHP allows us to change the directory in PHP. The function syntax is as shown:

chdir(string $directory): bool

The function accepts one parameter:

  1. directory - the path to the target directory as a string value.

The function returns a Boolean value with True on success and False failure.

Example Function Usage

The following example demonstrates how to use the chdir() function to change the working directory.


// current directory
echo getcwd() . "\n";


// current directory
echo getcwd() . "\n";


The above code should change the working directory from pwd to /var/www directory. Resulting output:



In this post, you learned how you can use the chdir() function to switch directories in PHP.

If you enjoyed these PHP tutorials, check out other resources down below.

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.