Is there a recommended script or method to automatically assign chmod permissions to subdirectories in a folder?

When working with directories in a folder, it can be tedious to manually assign chmod permissions to each subdirectory. One way to automate this process is by using a script or method to recursively assign permissions to all subdirectories within a folder. This can save time and ensure that all directories have the correct permissions set.

<?php
function chmod_recursive($dir, $mode) {
    $dh = opendir($dir);
    while (($file = readdir($dh)) !== false) {
        if ($file != '.' && $file != '..') {
            $path = $dir . '/' . $file;
            if (is_dir($path)) {
                chmod($path, $mode);
                chmod_recursive($path, $mode);
            }
        }
    }
    closedir($dh);
}

$dir = '/path/to/folder';
$mode = 0755; // Set the desired permissions here

chmod_recursive($dir, $mode);
?>