What are some alternative methods to set file permissions in PHP besides using flock()?

When setting file permissions in PHP, an alternative method to using flock() is to use the chmod() function. This function allows you to change the permissions of a file or directory by specifying the desired permissions as an octal value.

// Set file permissions using chmod()
$filename = 'example.txt';
$permissions = 0644; // Read and write for owner, read-only for others

if (file_exists($filename)) {
    chmod($filename, $permissions);
    echo "File permissions set successfully.";
} else {
    echo "File does not exist.";
}