rmdir() deletes an empty directory from the filesystem. This guide explains the function with focused examples and the decisions that matter in real applications.
What Is rmdir() in PHP?
Deletes an empty directory from the filesystem.
A typical call looks like this:
<?php
rmdir($directory, [$context]);
The input arguments shown in the call identify the relevant connection, resource, value, path or configuration for the operation. Pay attention to argument order and to the return value demonstrated below.
Example 1: Basic Usage
This first example uses rmdir() directly to demonstrate its basic purpose.
<?php
$path = sys_get_temp_dir() . "/demo-empty-dir"; mkdir($path); var_dump(rmdir($path));
The returned value illustrates deletes an empty directory from the filesystem. Nonempty directories cannot be removed with rmdir(); avoid recursive deletion by accident.
Example 2: A Different Input or Option
The next example changes the input or option, showing another supported use of rmdir().
<?php
$path = sys_get_temp_dir() . "/another-empty-dir"; mkdir($path); echo rmdir($path) ? "Removed" : "Failed";
The returned value illustrates deletes an empty directory from the filesystem. Nonempty directories cannot be removed with rmdir(); avoid recursive deletion by accident.
Practical Example: Applying the Function
In application code, rmdir() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.
<?php
$directoryPath = sys_get_temp_dir() . "/temp-report-dir"; mkdir($directoryPath); if (rmdir($directoryPath)) echo "Temporary directory cleaned";
The returned value illustrates deletes an empty directory from the filesystem. Nonempty directories cannot be removed with rmdir(); avoid recursive deletion by accident.
Important Behavior and Edge Cases
- Main behavior: Deletes an empty directory from the filesystem.
- Important limitation: Nonempty directories cannot be removed with rmdir(); avoid recursive deletion by accident.
- Requirements: Check the runtime, file permissions and operating system when results depend on the environment.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
rmdir() | Deletes an empty directory from the filesystem. |
mkdir | Creates a directory, whereas rmdir() deletes an empty directory. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the specific limits or preconditions of rmdir(). | Nonempty directories cannot be removed with rmdir(); avoid recursive deletion by accident. |
| Assuming every environment produces the same result. | Check the runtime, file permissions and operating system when results depend on the environment. |
Key Takeaways
- Deletes an empty directory from the filesystem.
- Nonempty directories cannot be removed with rmdir(); avoid recursive deletion by accident.
- Check the runtime, file permissions and operating system when results depend on the environment.
Use rmdir() when its behavior matches your task, and verify the relevant return value before taking further action. Run examples that affect processes, users, privileges or the filesystem only in a controlled development environment.