What are some best practices for troubleshooting and resolving permission denied errors when deleting directories in PHP on Windows?

When encountering permission denied errors when deleting directories in PHP on Windows, it is important to ensure that the appropriate permissions are set for the directory and that the PHP process has the necessary permissions to delete the directory. One common solution is to adjust the permissions of the directory or the parent directory to allow the PHP process to delete the directory.

<?php
$dir = 'path/to/directory';

// Check if the directory exists
if (is_dir($dir)) {
    // Set appropriate permissions for the directory
    chmod($dir, 0777);
    
    // Attempt to delete the directory
    if (rmdir($dir)) {
        echo 'Directory deleted successfully';
    } else {
        echo 'Failed to delete directory';
    }
} else {
    echo 'Directory does not exist';
}
?>