How can the umask() function in PHP help in resolving file permission issues?

When creating files or directories in PHP, sometimes there might be permission issues where the default file permissions are not what you want. The umask() function in PHP can help by allowing you to set the default file permissions for newly created files or directories. By using umask(), you can ensure that the permissions are set correctly from the start, reducing the chances of encountering permission issues later on.

// Set the umask to 002 so that newly created files have permissions 664 and directories have permissions 775
umask(002);

// Create a new file with the correct permissions
$file = fopen('newfile.txt', 'w');
fclose($file);

// Create a new directory with the correct permissions
mkdir('newdir', 0777);