How can the chmod() function be used to set file permissions in PHP?

The chmod() function in PHP can be used to set file permissions by specifying the file path and the desired permissions in numeric format. This function allows you to control who can read, write, or execute the file. To use chmod(), you need to have the necessary permissions to modify the file's permissions.

<?php
$file = 'example.txt';
$permissions = 0644; // For example, read and write for owner, read for others
if (chmod($file, $permissions)) {
    echo "File permissions changed successfully.";
} else {
    echo "Failed to change file permissions.";
}
?>