ftp_rmdir()
The ftp_rmdir() function is a built-in PHP function that removes a directory on an FTP server. In this article, we'll discuss the function in detail and provide
Understanding the PHP Function ftp_rmdir()
The ftp_rmdir() function is a built-in PHP function that removes a directory on an FTP server. In this article, we'll discuss the function in detail and provide you with a comprehensive guide to using it in your PHP projects.
What is ftp_rmdir()?
The ftp_rmdir() function removes a directory on an FTP server. It is the counterpart to ftp_mkdir(), which creates one.
A key constraint: it only works on empty directories. If the directory still contains files or subdirectories, the call fails and returns false. To remove a non-empty directory you must delete its contents first (see the recursive example below).
The function takes two parameters:
ftp— the FTP connection returned byftp_connect()(and authenticated withftp_login()).directory— the path of the directory to delete.
It returns true on success and false on failure.
Syntax of ftp_rmdir()
The signature of the ftp_rmdir() function is:
ftp_rmdir(FTP\Connection $ftp, string $directory): boolBoth parameters are required. Note that in PHP 8.1 and later the connection is an FTP\Connection object; before PHP 8.1 it was a resource. The directory parameter is the name (relative to the current directory) or absolute path of the directory to remove.
Usage of ftp_rmdir()
To use the ftp_rmdir() function, you first need to establish a connection to the FTP server using the ftp_connect() function. Here's a complete example:
<?php
// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');
if (!$conn) {
die("Could not connect to FTP server.");
}
// Login with your FTP credentials
if (!ftp_login($conn, 'username', 'password')) {
die("Login failed.");
}
// Remove the directory
if (ftp_rmdir($conn, '/public_html/testdir')) {
echo "Directory removed successfully.\n";
} else {
echo "Directory removal failed. Ensure the directory is empty.\n";
}
// Close the FTP connection
ftp_close($conn);Step by step: we open a connection with ftp_connect() and verify it; log in with ftp_login(); remove the directory with ftp_rmdir() and report the result; then close the connection with ftp_close().
Removing a non-empty directory recursively
Because ftp_rmdir() refuses non-empty directories, removing a folder tree means deleting every file and subdirectory first. You can list the contents with ftp_nlist(), delete files with ftp_delete(), and recurse into subdirectories:
<?php
function ftpRemoveTree($conn, string $dir): bool
{
// ftp_delete handles files; ftp_rmdir handles (now-empty) directories.
if (@ftp_delete($conn, $dir)) {
return true;
}
// Not a file — assume it's a directory and clear its contents.
$items = ftp_nlist($conn, $dir);
if ($items === false) {
return false;
}
foreach ($items as $item) {
// Skip the "." and ".." entries some servers return.
$name = basename($item);
if ($name === '.' || $name === '..') {
continue;
}
ftpRemoveTree($conn, $item);
}
// Directory is empty now, so it can be removed.
return ftp_rmdir($conn, $dir);
}Error handling in ftp_rmdir()
It's important to handle errors when using ftp_rmdir(). A false return means the removal failed — most often because the directory is not empty, does not exist, or your account lacks permission. Always check the return value rather than assuming success:
<?php
if (!ftp_rmdir($conn, '/public_html/testdir')) {
echo "Failed to remove directory. Ensure it is empty and you have proper permissions.\n";
}
ftp_close($conn);Related FTP functions
ftp_mkdir()— create a directory.ftp_delete()— delete a single file.ftp_nlist()— list the contents of a directory.ftp_chdir()— change the current working directory.ftp_pwd()— get the current directory path.
Conclusion
The ftp_rmdir() function removes an empty directory on an FTP server, returning true on success and false on failure. To remove a directory that still holds files, clear its contents first — for example with the recursive helper above. Combined with careful return-value checks, it is a reliable building block for managing remote directory structures from PHP.