What is the significance of umask in relation to setting file permissions in PHP?

The umask in PHP is used to set default file permissions for newly created files and directories. It works by subtracting the specified umask value from the maximum permissions to determine the actual permissions that will be set on the new file or directory. By setting the umask appropriately, you can ensure that files are created with the desired permissions, such as restricting access to certain users or groups.

// Set the umask to restrict permissions for newly created files
umask(0022);

// Create a new file with restricted permissions
$file = fopen("example.txt", "w");
fwrite($file, "Hello, World!");
fclose($file);

// Check the permissions of the newly created file
$permissions = fileperms("example.txt");
echo substr(sprintf('%o', $permissions), -4); // Output: 0644