PHP | rmdir( ) Function

The rmdir() function in PHP is an inbuilt function which is used to remove an empty directory. It is mandatory for the directory to be empty, and it must have the relevant permissions which are required to delete the directory.
The directory to be deleted is sent as a parameter to the rmdir() function and it returns True on success or False on failure.

Syntax:

rmdir(dirname, context)

Parameters Used:
The rmdir() function in PHP accepts two parameters.



  1. dirname : It is a mandatory parameter which specifies the directory to be deleted.
  2. context : It is an optional parameter which specifies the behavior of the stream .

Return Value:
It returns True on success or False on failure.

Errors And Exception

  1. The rmdir() function generates an E_WARNING level error on failure.
  2. opendir() must be closed before using rmdir() function else it gives permission denied error.
  3. PHP checks whether the directory in which the script is operating has the same UID (owner) as the script that is being executed when it is in safe mode.

Examples:

Input : mkdir('gfg');
        $dirname= "gfg";
        rmdir($dirname);
Output : 1

Input : $dirname = "gfg";
        if(rmdir($dirname))
        {
          echo ("$dirname successfully removed");
        }
        else
        {
          echo ("$dirname couldn't be removed"); 
        }
Output : gfg successfully removed

Below programs illustrate the rmdir() function.

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// creating a directory named gfg
mkdir('gfg');
$dirname= "gfg";
  
// removing directory using rmdir()
rmdir($dirname);
?>

chevron_right


Output:

1

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// creating a directory named gfg
 $dirname = "gfg";
  
// removing directory using rmdir()
if(rmdir($dirname))
{
  echo ("$dirname successfully removed");
}
else
{
 echo ($dirname . "couldn't be removed"); 
}
?>

chevron_right


Output:

gfg successfully removed

Reference:
http://php.net/manual/en/function.rmdir.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.