Home  >  Article  >  Backend Development  >  Detailed explanation of how to use the PHP rmdir() function

Detailed explanation of how to use the PHP rmdir() function

藏色散人
藏色散人Original
2019-04-17 17:27:072503browse

The rmdir() function in PHP is a built-in function used to delete empty directories. The directory must be empty, and you must have the relevant permissions required to delete the directory.

The directory to be deleted is sent to the rmdir() function as a parameter. If it succeeds, it will return True; if it fails, it will return False.

Syntax:

rmdir(dirname, context)

Usage of parameters:

The rmdir() function in PHP accepts two parameter.

dirname: It is a mandatory parameter that specifies the directory to be deleted.

context: It is an optional parameter that specifies the behavior of the stream.

Return value:

Returns True when successful and False when failed.

Errors and exceptions

1. The rmdir() function generates an E_WARNING level error when it fails.

2. Opendir() must be closed before using the rmdir() function, otherwise a permission denied error will be given.

3. PHP checks whether the directory where the script is running has the same UID (owner) as the script being executed in safe mode.

rmdir() function code example 1:

<?php 
// 创建一个名为gfg的目录
mkdir(&#39;gfg&#39;); 
$dirname= "gfg"; 
  
// 使用rmdir()删除目录
rmdir($dirname); 
?>

Output:

1

rmdir() function code example 2:

<?php 
// 创建一个名为gfg的目录
 $dirname = "gfg"; 
  
// 使用rmdir()删除目录
if(rmdir($dirname)) 
{ 
  echo ("$dirname已成功删除"); 
} 
else
{ 
 echo ($dirname . "不能被删除");  
} 
?>

Output:

gfg已成功删除

Related recommendations: "PHP Tutorial"

The above is the detailed content of Detailed explanation of how to use the PHP rmdir() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn