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:
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 "目录不存在。"; }
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!