What is the rmdir() function in PHP?

The rmdir() function in PHP removes an empty directory.

Syntax

rmdir(string $path, resource $context = ?): bool

Parameters

path: This specifies the path of the directory we want to remove.

context: This is an optional parameter that specifies the context of the file handle. It is a set of options that modify the system’s behavior.

Return value

rmdir() returns true if the directory is removed successfully. Otherwise, in case of any error, it returns false.

Code

The following code shows how to use the rmdir() function.

<?php
mkdir("documents");
if(rmdir("documents"))
{
echo ("Directory removed");
}
?>

Explanation

In the code above, a directory named documents is created. It is then removed successfully using the rmdir function since it is an empty directory.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved