What is the significance of the "Directory not empty" warning in PHP when trying to delete a directory?

The "Directory not empty" warning in PHP occurs when trying to delete a directory that still contains files or subdirectories. To solve this issue, you need to first recursively delete all files and subdirectories within the directory before attempting to delete the directory itself.

function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return true;
    }

    if (!is_dir($dir)) {
        return unlink($dir);
    }

    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') {
            continue;
        }

        if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
            return false;
        }
    }

    return rmdir($dir);
}

// Usage
$directory = 'path/to/directory';
deleteDirectory($directory);