PHP function introduction—rmdir(): delete directory

王林
Release: 2023-07-26 13:22:01
Original
1642 people have browsed it

PHP function introduction—rmdir(): Delete directory

Introduction:
In PHP, the rmdir() function is used to delete the specified directory. It is a very useful function that is often used when working with files and directories. The rmdir() function can delete an empty directory, but it cannot delete a non-empty directory.

Usage syntax:
bool rmdir ( string $path [, resource $context ] )

Parameter description:

  • path: the directory path to be deleted (required). Can be an absolute or relative path.
  • context: Optional parameter used to specify the context of the stream.

Return value:
If the directory is deleted successfully, it returns true; if the deletion fails, it returns false.

Note:
When deleting a directory, make sure the directory is empty, otherwise the deletion operation will fail. If the directory is not empty, you can use other functions (such as deleting all files and folders in the directory) to clear the directory, and then call the rmdir() function to delete the directory.

Code example:
The following is a simple example that demonstrates how to use the rmdir() function to delete an empty directory.

$dir = 'path/to/directory';

// 检查目录是否存在
if (is_dir($dir)) {
    // 删除目录
    if (rmdir($dir)) {
        echo "目录删除成功。";
    } else {
        echo "目录删除失败。";
    }
} else {
    echo "目录不存在。";
}
Copy after login

In this example, we first use the is_dir() function to check whether the specified directory exists. If the directory exists, we use the rmdir() function to delete the directory. If the deletion is successful, "Directory deletion successful" is output; if the deletion fails, "Directory deletion failed" is output; if the directory does not exist, "Directory does not exist" is output.

Summary:
The rmdir() function is a function used to delete directories in PHP, which is very convenient and practical. But it should be noted that this function can only delete empty directories. If you want to delete a non-empty directory, you need to delete all files and subdirectories in the directory first, and then call the rmdir() function to delete the directory itself.

The above is the detailed content of PHP function introduction—rmdir(): delete directory. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template