PHP rmdir() Function

What is PHP rmdir() Function?

To delete an empty directory in your system, use rmdir() function. If there exists any content inside the directory, the function doesn’t work. So, you must make the directory empty before attempting to removing with this function. “rmdir” is short for “Remove Directory”.

Syntax:

rmdir(directory, context)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

directory (Required): The path to the directory that you want to remove. It could be an absolute path or relative to the current directory. Check example 1.

context (Optional): A context stream resource. It allows you to define context of the directory handle. Context is a set of options that can modify the behavior of a stream.

Return Values:

The function returns –

  • TRUE – If it removes the directory. Check example 2.
  • FALSE – If it can’t remove it. PHP also generates an E_WARNING level error. Check example 3.

Examples:

Example 1: Removing a directory-

<?php
rmdir("./images/temp");
?>

Explanation:

Here, the function removes the directory “temp” which is inside the “images” folder. The folder “image” is in same level as this script is.

Example 2: Removing a directory with confirmation-

<?php
if(rmdir("./images/temp")){
    echo "Directory removed successfully.";
}else{
    echo "ERROR! Directory not removed.";
}
?>

Output:

Directory removed successfully.

Explanation:

As the directory exists in the specified path, the function rmdir() remove it and returns TRUE, and the program prints successful message. If the function could not remove the directory, it returns false.

Example 3: Displaying warning on failure to remove a directory-

<?php
rmdir("./images/noFolder");
?>

Output:

Warning: rmdir(./images/noFolder): No such file or directory in D:\xampp\htdocs\php\rmdir.php on line 2

Explanation:

As there is no folder named “noFolder” in the specified path, PHP generates a warning.

Example 4: Checking directory existence before removing it-

<?php
if(is_dir("./images/temp")){
    if(rmdir("./images/temp")){
        echo "Directory removed successfully.";
}else{
    echo "It is not a directory.";
}
?>

Output:

Directory removed successfully.

Explanation:

Before executing the rmdir() function, the script first checks whether the specified directory is a directory itself with the is_dir() function. It confirms its existence. Then, the program removes the directory. It saves you from “No such file or folder” Warning.

Example 5: Removing a directory when it is opened-

<?php
$dir_handle = opendir("./images/temp");
rmdir("./images/temp");
?>

Explanation:

The rmdir() function removes the  “temp” directory when the directory is already opened by the opendir() function in the previous line and a directory handle is created.

Best Practices on rmdir() Function:

  • To avoiding getting error, before attempting to remove an empty folder, always check whether the folder exists with is_dir() function. Check example 4.

Caution:

  • Check whether the directory has remove permission granted, otherwise you can’t remove it.
  • rmdir() function can still remove a directory even though you open the directory with opendir() function. Check example 5.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP () Function

remir(), which is one of the built-in directory functions, is a very useful and common function when you want to remove a directory and it is also very easy to implement too.

Reference:

https://www.php.net/manual/en/function.rmdir.php