What are the best practices for handling file permissions in PHP scripts?

When handling file permissions in PHP scripts, it is important to ensure that sensitive files are not accessible to unauthorized users. To do this, you should set appropriate file permissions using functions like chmod(). It is recommended to restrict access to files by setting permissions to only allow read, write, or execute access to specific users or groups.

// Set file permissions to only allow read and write access for the owner
$file = 'example.txt';
$permissions = 0600; // Owner can read and write
chmod($file, $permissions);