What is the best practice for changing file permissions (chmod) in PHP?

When changing file permissions (chmod) in PHP, it is important to ensure that the permissions are set correctly to maintain security and access control. The best practice is to use the chmod function provided by PHP, which takes the file path and the desired permissions as arguments. It is recommended to use octal notation to specify the permissions (e.g., 0644 for read and write permissions for the owner and read-only permissions for others).

$file = 'path/to/file.txt';
$permissions = 0644;

if (chmod($file, $permissions)) {
    echo "File permissions changed successfully.";
} else {
    echo "Failed to change file permissions.";
}