PHP ftp_rename() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_rename() renames or moves a file or folder on a remote FTP server.

You give it the current name and the new name. If the new name includes a different folder, most servers will move the file there, as long as that folder already exists.

Syntax

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

PHP
$ok = ftp_rename($ftp, 'old.txt', 'new.txt');
CallWhat it does
ftp_rename($ftp, 'old.txt', 'new.txt')Renames old.txt to new.txt in the current folder.
ftp_rename($ftp, 'a.txt', 'archive/a.txt')Moves a.txt into the archive folder, if it exists.
ftp_rename($ftp, 'images', 'photos')Renames a folder.

ftp_rename() works on the server only. It does not touch your local files. Whether an existing file with the new name is replaced depends on the server, so check with ftp_size() first.

Library declarations
PHP
ftp_rename(FTP\Connection $ftp, string $from, string $to): bool

Parameters

ParameterDescription
ftpRequired. The FTP connection returned by ftp_connect() or ftp_ssl_connect(). You should already be logged in.
fromRequired. The current remote file or folder name.
toRequired. The new remote name or path.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Rename a File

Rename a file on the server from draft.txt to final.txt.

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

if (ftp_rename($ftp, 'draft.txt', 'final.txt')) {
    echo "Renamed to final.txt", PHP_EOL;
} else {
    echo "Rename failed", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The connection is opened and you log in as usual.
  2. ftp_rename() sends the old name and the new name to the server.
  3. The server changes the name and the function returns true.
  4. The success message is printed and the connection is closed.

Example 2 — Check That the Source Exists

Use ftp_size() to confirm the file is there before you try to rename it.

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

$from = 'missing.txt';

if (ftp_size($ftp, $from) === -1) {
    echo "$from does not exist", PHP_EOL;
} else {
    ftp_rename($ftp, $from, 'renamed.txt');
    echo "Renamed", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_size() returns -1 when the file cannot be found.
  2. The script checks that value before calling ftp_rename().
  3. Since the file is missing, a clear message is shown.
  4. No rename is attempted, so there is no warning from PHP.

Example 3 — Move a File to Another Folder

Give ftp_rename() a new path to move a file into an archive folder.

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

@ftp_mkdir($ftp, 'archive');

if (ftp_rename($ftp, 'report.pdf', 'archive/report.pdf')) {
    echo "Moved report.pdf into archive", PHP_EOL;
}

print_r(ftp_nlist($ftp, 'archive'));

ftp_close($ftp);

How It Works

  1. ftp_mkdir() makes sure the archive folder exists, and the @ hides a warning if it already does.
  2. ftp_rename() uses a new path, which tells the server to move the file.
  3. ftp_nlist() lists the archive folder so we can confirm the move.
  4. The listing shows the file now lives inside archive.

Frequently Asked Questions

It renames a file or folder on a remote FTP server. It can also move an item if the new name includes another existing folder.
Yes, on most servers. Give the full new path as the second argument, and make sure the target folder already exists.
The source may not exist, the target folder may be missing, or you may lack permission. Some servers also refuse when the new name is already taken.
It depends on the server. Some replace the file, others refuse. Check with ftp_size() first if you want to be safe.
No. It only works on the remote server. Use PHP's rename() function for local files.

Next: ftp_rmdir()

ftp_rmdir() removes an empty directory from a remote FTP server.

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