W3docs

ftp_pwd()

The ftp_pwd() function is a built-in PHP function that returns the current directory of the FTP connection. In this article, we'll discuss the function in

The PHP ftp_pwd() function returns the current working directory of an open FTP connection — the equivalent of typing pwd ("print working directory") in an FTP client. This page explains what the function returns, how to call it correctly across PHP versions, and how it fits into a typical FTP session.

What ftp_pwd() does

After you connect and log in to an FTP server, the server keeps track of a "current directory" for your session. As you move around with ftp_chdir() or ftp_cdup(), that location changes. ftp_pwd() tells you where you currently are, returning the absolute path as a string.

This is mostly useful for:

  • Logging or debugging — confirming you landed in the directory you expected after login.
  • Saving and restoring a location: capture the path with ftp_pwd(), change directories to do some work, then ftp_chdir() back.
  • Building paths for uploads/downloads relative to the server's current location.

Syntax

ftp_pwd(FTP\Connection $ftp): string|false

It takes a single argument:

It returns the current directory as a string on success, or false on failure.

Version note: In PHP 8.1 and later, the connection is an FTP\Connection object. In PHP 7.x and earlier, FTP functions used a resource instead — the calling code is identical, only the type of $ftp changed. Code written for older versions keeps working without modification.

Basic usage

You must connect and log in before calling ftp_pwd():

<?php

// Open an FTP connection
$ftp = ftp_connect('ftp.example.com');

// Log in with your credentials
ftp_login($ftp, 'username', 'password');

// Ask the server where we are
$current = ftp_pwd($ftp);

echo "Current directory: $current\n"; // e.g. "Current directory: /"

// Always close the connection when done
ftp_close($ftp);

Right after login most servers place you in the account's home directory, so the first ftp_pwd() call commonly returns / or something like /home/username.

Tracking your location after changing directories

The value ftp_pwd() returns reflects every navigation step. Combined with ftp_chdir() it lets you confirm a move succeeded:

<?php

$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

echo ftp_pwd($ftp) . "\n";        // /

ftp_chdir($ftp, 'public_html');
echo ftp_pwd($ftp) . "\n";        // /public_html

ftp_chdir($ftp, 'images');
echo ftp_pwd($ftp) . "\n";        // /public_html/images

ftp_close($ftp);

Saving and restoring the working directory

A common pattern is to remember where you are, do work elsewhere, then return:

<?php

$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

// Remember the starting point
$home = ftp_pwd($ftp);

// Move into a subfolder and upload a file
ftp_chdir($ftp, 'uploads');
ftp_put($ftp, 'report.pdf', '/tmp/report.pdf', FTP_BINARY);

// Go back to where we started
ftp_chdir($ftp, $home);

ftp_close($ftp);

Error handling

ftp_pwd() returns false if the connection is invalid or the server rejects the request. Because an empty string is also falsy, use a strict === false check rather than !$current:

<?php

$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

$current = ftp_pwd($ftp);

if ($current === false) {
    echo "Failed to get the current directory.\n";
} else {
    echo "You are in: $current\n";
}

ftp_close($ftp);

Note that ftp_connect() itself returns false if it cannot reach the host, so in production code you should check the connection and login results before calling ftp_pwd().

Practice

Practice
What is the role of the ftp_pwd function in PHP?
What is the role of the ftp_pwd function in PHP?
Was this page helpful?