PHP ftp_chdir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_chdir() changes the current working directory on the FTP server to the folder you name.

It works like cd in a terminal. After it succeeds, any relative paths you give to functions such as ftp_get(), ftp_put() or ftp_nlist() are resolved from this new folder.

Syntax

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

PHP
$ok = ftp_chdir($ftp, "/public_html/uploads");
CallWhat it does
ftp_chdir($ftp, "/public_html")Moves to an absolute directory path and returns true on success.
ftp_chdir($ftp, "images")Moves into a sub folder relative to the current directory.
ftp_chdir($ftp, "..")Moves up one level, similar to ftp_cdup().

From PHP 8.1 the first argument is an FTP\Connection object. ftp_chdir() raises a warning if the folder does not exist, so check the return value or use the @ operator carefully.

Library declarations
PHP
ftp_chdir(FTP\Connection $ftp, string $directory): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
directoryRequired. The target directory. It can be an absolute path like /public_html or a path relative to the current folder.

Return Value

Return ValueDescription
booltrue on success, false on failure (for example when the folder does not exist).

PHP Examples

Example 1 — Change to a Folder

Move into a folder and confirm the new location. The host and login are placeholders.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

if (ftp_chdir($ftp, "/public_html/uploads")) {
    echo "Current folder: ", ftp_pwd($ftp), PHP_EOL;
} else {
    echo "Could not change folder.", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. We connect and log in first.
  2. ftp_chdir() tries to move into /public_html/uploads.
  3. On success ftp_pwd() prints the new current directory.
  4. Otherwise the script prints an error message.

Example 2 — A Folder That Does Not Exist

Trying to enter a missing folder returns false and raises a warning. Use the @ operator to hide the warning and handle it yourself.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$ok = @ftp_chdir($ftp, "/no/such/folder");

var_dump($ok);
echo "Still in: ", ftp_pwd($ftp), PHP_EOL;

ftp_close($ftp);

How It Works

  1. The folder /no/such/folder does not exist on the server.
  2. ftp_chdir() returns false, and the @ operator hides the PHP warning.
  3. The current directory does not change.
  4. ftp_pwd() confirms we are still in the root folder.

Example 3 — Change Folder, Then List Files

After ftp_chdir() you can use relative paths, such as ftp_nlist($ftp, ".") for the current folder.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

ftp_chdir($ftp, "/public_html/images");

$files = ftp_nlist($ftp, ".");

foreach ($files as $file) {
    echo $file, PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_chdir() moves into the images folder.
  2. ftp_nlist() with a dot lists the contents of the current folder.
  3. It returns an array of file and folder names.
  4. foreach prints each name on its own line.

Frequently Asked Questions

It changes the current working directory on the FTP server. Later FTP calls with relative paths start from that folder.
PHP raises a warning when the server refuses to change into the folder, for example when it does not exist or you lack permission. Check the return value and handle the false case.
Yes. A path starting with a slash, like /public_html, is absolute. Without the slash it is relative to the current directory.
Use ftp_cdup($ftp) or ftp_chdir($ftp, '..'). Both move to the parent directory.
No. It only changes the folder on the remote FTP server. Use chdir() for the local PHP working directory.

Next: ftp_chmod()

ftp_chmod() sets the permissions of a file or folder on the FTP server. It sends the SITE .

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