What are the limitations of using chmod() on directories in PHP?

When using chmod() on directories in PHP, there is a limitation where the permissions set may not apply recursively to all files and subdirectories within the directory. To ensure that permissions are set recursively, you can use the PHP function chmod_recursive() which will apply the specified permissions to all files and subdirectories within the target directory.

function chmod_recursive($path, $file_perm, $dir_perm) {
    $dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
    $files = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
    
    foreach ($files as $file) {
        if ($file->isDir()) {
            chmod($file, $dir_perm);
        } else {
            chmod($file, $file_perm);
        }
    }
}

// Example usage
chmod_recursive('/path/to/directory', 0644, 0755);