What potential pitfalls should be considered when deleting directories in PHP?

When deleting directories in PHP, potential pitfalls to consider include ensuring that the directory is empty before deletion, handling permissions to avoid deleting important files, and verifying that the directory exists before attempting to delete it.

// Check if directory exists before attempting to delete
$directory = 'path/to/directory';
if (is_dir($directory)) {
    // Check if directory is empty before deletion
    if (count(glob("$directory/*")) === 0) {
        // Delete directory
        rmdir($directory);
        echo 'Directory deleted successfully.';
    } else {
        echo 'Directory is not empty.';
    }
} else {
    echo 'Directory does not exist.';
}