PHP ftp_pwd() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_pwd() returns the current working directory on the FTP server. PWD stands for print working directory.

After you log in, you are placed in a starting folder. Each time you use ftp_chdir() or ftp_cdup(), that folder changes, and ftp_pwd() tells you where you are now.

Syntax

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

PHP
$dir = ftp_pwd($ftp);
CallWhat it does
ftp_pwd($ftp)Returns the current remote folder, such as "/" or "/public_html".
ftp_chdir($ftp, 'images'); ftp_pwd($ftp)Returns the new path after you change into a sub folder.
ftp_pwd($ftp) === falseChecks whether the server failed to report its directory.

ftp_pwd() takes only the connection and returns a string. It is a good way to confirm that ftp_chdir() really moved you to the folder you expected.

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

Parameters

ParameterDescription
ftpRequired. The FTP connection returned by ftp_connect() or ftp_ssl_connect(). You should already be logged in.

Return Value

Return ValueDescription
string|falseReturns the current directory path as a string, or false if the command fails.

PHP Examples

Example 1 — Show the Starting Directory

Log in and print the folder the server puts you in.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

echo "Current folder: ", ftp_pwd($ftp), PHP_EOL;

ftp_close($ftp);

How It Works

  1. ftp_connect() and ftp_login() open the session.
  2. ftp_pwd() asks the server for the current working directory.
  3. The server answers with a path, here the root folder.
  4. echo prints that path and ftp_close() ends the session.

Example 2 — Check for a Failed Result

ftp_pwd() can return false, so test the result with the strict comparison operator.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

$dir = ftp_pwd($ftp);

if ($dir === false) {
    echo "Could not read the directory", PHP_EOL;
} else {
    echo "You are in: $dir", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The result of ftp_pwd() is saved in $dir.
  2. === false makes sure an empty or odd path is not mistaken for a failure.
  3. If the command failed, the first branch would print an error message.
  4. Here the server answered normally, so the path is printed.

Example 3 — Track the Folder While Moving

Print the directory before and after ftp_chdir() to see how the path changes.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

echo "Before: ", ftp_pwd($ftp), PHP_EOL;

if (ftp_chdir($ftp, 'public_html')) {
    echo "After: ", ftp_pwd($ftp), PHP_EOL;
}

ftp_cdup($ftp);
echo "Back: ", ftp_pwd($ftp), PHP_EOL;

ftp_close($ftp);

How It Works

  1. The first ftp_pwd() shows the root folder where the session starts.
  2. ftp_chdir() moves into public_html and returns true.
  3. The second ftp_pwd() now reports /public_html.
  4. ftp_cdup() goes up one level, so the last ftp_pwd() prints the root again.

Frequently Asked Questions

It returns the current working directory on the FTP server, so you know which remote folder your next commands will use.
It returns false. Compare the result with === false so a valid path is never confused with a failure.
No. It only reads the current folder. Use ftp_chdir() to move into another folder or ftp_cdup() to go up one level.
The path belongs to the remote server. Many hosts show a virtual root, so you might see / even though the real folder is deeper on the server.
Yes. You should call ftp_login() first. Most servers refuse directory commands until you are authenticated.

Next: ftp_quit()

ftp_quit() closes an FTP connection. It is an alias of ftp_close(), so both functions do e.

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