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

When managing file permissions in PHP scripts, it is important to follow best practices to ensure the security and integrity of your files. This includes setting appropriate permissions for files and directories, avoiding using overly permissive permissions, and regularly reviewing and updating permissions as needed.

// Set appropriate permissions for a file
$file = 'example.txt';
$permissions = 0644; // Read and write for owner, read for everyone else
if (file_exists($file)) {
    chmod($file, $permissions);
} else {
    echo "File does not exist.";
}