PHP Function Reference

PHP rmdir() Function



The PHP rmdir() function attempts to remove the specified directory. The directory must be empty, and must have the relevant permissions to perform the activity.

Syntax

rmdir(directory, context)

Parameters

directory Required. Specify the path to the directory to be removed.
context Optional. Specify the context of the file handle. Context is a set of options that can modify the behavior of a stream.

Return Value

Returns true on success or false on failure.

Exceptions

On failure, an E_WARNING level error is thrown.

Example:

In the example below, rmdir() function is used to delete the specified directory.

<?php
$dir = "temp";

//creating a folder in the current directory
if(mkdir($dir)) {
  echo "$dir directory is successfully created.\n";
} else {
  echo "Can not create $dir directory.\n";
}

if(rmdir($dir)) {
  echo "$dir directory is successfully deleted.\n";
} else {
  echo "Can not delete $dir directory.\n";
}
?>

The output of the above code will be:

temp directory is successfully created.
temp directory is successfully deleted.

❮ PHP Filesystem Reference