What role does umask play in setting directory permissions in PHP and how can it be manipulated to achieve desired results?

The umask function in PHP plays a role in setting default permissions for newly created directories. By manipulating the umask value, you can control the permissions that are automatically set when a new directory is created. To achieve desired results, you can change the umask value before creating a directory and then restore it back to its original value.

// Set the umask value to allow specific permissions for directories
$original_umask = umask(0); // Disable umask temporarily
mkdir('/path/to/directory', 0777, true); // Create directory with desired permissions
umask($original_umask); // Restore original umask value