PHP ftp_cdup() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_cdup() changes the current directory on the FTP server to the parent directory. It is the same as going up one folder level.

Think of it like typing cd .. in a terminal. After the call, relative paths you use in other FTP functions start from the new, higher folder.

Syntax

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

PHP
$ok = ftp_cdup($ftp);
CallWhat it does
ftp_cdup($ftp)Moves up one directory and returns true on success.
ftp_chdir($ftp, "images"); ftp_cdup($ftp);Goes into a folder and then back to where we started.
ftp_cdup($ftp); ftp_pwd($ftp)Moves up and then reads the new current directory.

ftp_cdup() takes only the connection. From PHP 8.1 that connection is an FTP\Connection object. Use ftp_pwd() afterwards if you want to see where you ended up.

Library declarations
PHP
ftp_cdup(FTP\Connection $ftp): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Go Up One Folder

Enter a sub folder and then move back up with ftp_cdup(). The host and login are placeholders.

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

ftp_chdir($ftp, "/public_html/images");
echo "Now in: ", ftp_pwd($ftp), PHP_EOL;

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

ftp_close($ftp);

How It Works

  1. ftp_chdir() moves into the images folder.
  2. ftp_pwd() prints the current directory path.
  3. ftp_cdup() moves up one level to public_html.
  4. ftp_pwd() confirms the new location.

Example 2 — Already at the Root

Calling ftp_cdup() at the top of the server may fail or stay in place, so check the result.

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

ftp_chdir($ftp, "/");

$moved = ftp_cdup($ftp);
var_dump($moved);
echo "Still in: ", ftp_pwd($ftp), PHP_EOL;

ftp_close($ftp);

How It Works

  1. We go to the root directory with ftp_chdir().
  2. ftp_cdup() has nowhere higher to go.
  3. Many servers still answer true but remain at the root; others return false.
  4. ftp_pwd() shows we are still in /.

Example 3 — Walk Up to the Top

Use a loop to keep going up until the current directory is the root.

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

ftp_chdir($ftp, "/a/b/c");

while (ftp_pwd($ftp) !== "/") {
    echo "At ", ftp_pwd($ftp), PHP_EOL;
    ftp_cdup($ftp);
}

echo "Reached root", PHP_EOL;

ftp_close($ftp);

How It Works

  1. We start three levels deep at /a/b/c.
  2. The while loop checks the current path with ftp_pwd().
  3. Each pass prints the path and calls ftp_cdup() to go up once.
  4. When the path becomes /, the loop stops and we print a final message.

Frequently Asked Questions

It changes the current FTP directory to the parent directory, just like cd .. in a command line.
They do the same job. ftp_cdup() sends the special CDUP command, which some servers handle better than a plain CWD to a parent path.
Call ftp_pwd($ftp). It returns the current directory path as a string, or false on failure.
No. You only pass the FTP\Connection object. It always moves up exactly one level.
It depends on the server. Some return false, others return true and stay in the root. Always confirm with ftp_pwd() if it matters.

Next: ftp_chdir()

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

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