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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved