Tutorialspoint.dev

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

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

Output:

1

Program 2

<?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"); 
}
?>

Output:

gfg successfully removed

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter