PHP ftp_rmdir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

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

It is the opposite of ftp_mkdir(). The folder must be empty first, so you normally delete the files inside it with ftp_delete() before removing the folder.

Syntax

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

PHP
$ok = ftp_rmdir($ftp, 'old-folder');
CallWhat it does
ftp_rmdir($ftp, 'temp')Removes the folder named temp if it is empty.
ftp_rmdir($ftp, '/uploads/2024')Removes a folder using a full remote path.
ftp_delete($ftp, 'temp/a.txt'); ftp_rmdir($ftp, 'temp')Deletes the file first, then removes the now-empty folder.

ftp_rmdir() cannot delete a folder that still has files or sub folders in it. To clear a full folder tree, you need to loop through the contents and delete them one by one.

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

Parameters

ParameterDescription
ftpRequired. The FTP connection returned by ftp_connect() or ftp_ssl_connect(). You should already be logged in.
directoryRequired. The remote directory to remove. It must be empty.

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Remove an Empty Folder

Create a temporary folder and then remove it again.

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

ftp_mkdir($ftp, 'temp');
echo "Created temp", PHP_EOL;

if (ftp_rmdir($ftp, 'temp')) {
    echo "Removed temp", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_mkdir() makes a new empty folder called temp.
  2. ftp_rmdir() is then called on the same folder.
  3. Because it is empty, the server allows the removal.
  4. The function returns true, so the second message is printed.

Example 2 — A Folder That Is Not Empty

ftp_rmdir() fails on folders that still contain files. Check the result and explain why.

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

if (@ftp_rmdir($ftp, 'uploads')) {
    echo "Removed uploads", PHP_EOL;
} else {
    echo "Could not remove uploads (is it empty?)", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The uploads folder still contains files.
  2. ftp_rmdir() asks the server to remove it, but the server refuses.
  3. The function returns false and PHP would normally show a warning.
  4. The @ hides that warning so the script can print a friendlier message.

Example 3 — Empty a Folder, Then Remove It

List the files, delete each one, and finally remove the folder.

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

$folder = 'cache';
$files  = ftp_nlist($ftp, $folder) ?: [];

foreach ($files as $file) {
    ftp_delete($ftp, $file);
    echo "Deleted $file", PHP_EOL;
}

echo ftp_rmdir($ftp, $folder) ? "Folder removed" : "Failed", PHP_EOL;

ftp_close($ftp);

How It Works

  1. ftp_nlist() returns the names of everything inside the cache folder.
  2. The ?: [] part gives an empty array if the listing fails.
  3. ftp_delete() removes each file one by one.
  4. Once the folder is empty, ftp_rmdir() can remove it.

Frequently Asked Questions

It removes an empty folder from an FTP server. It returns true when the folder is removed and false when something goes wrong.
The most common reason is that the folder is not empty. Other reasons are a wrong path, a missing folder, or no permission to delete.
No. Delete all files with ftp_delete() and remove sub folders first. Then call ftp_rmdir() on the empty folder.
No. Once the folder is gone, you must create it again with ftp_mkdir(). Be careful with the path you pass in.
ftp_delete() removes a single file, while ftp_rmdir() removes an empty folder.

Next: ftp_set_option()

ftp_set_option() changes a setting of an FTP connection, such as the timeout or whether fi.

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