How does umask function in PHP help in managing file permissions?

umask function in PHP helps in managing file permissions by allowing you to set the default file permissions for newly created files and directories. By using umask, you can restrict the permissions that are automatically assigned to files, ensuring that they adhere to your security requirements.

// 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 "Permissions for example.txt: " . decoct($permissions & 0777);