What are some best practices for managing file permissions in PHP scripting?

When managing file permissions in PHP scripting, it is important to ensure that the correct permissions are set to prevent unauthorized access or modifications to files. One best practice is to set the appropriate permissions using chmod() function to restrict access to files and directories based on their intended use.

// Set file permissions to restrict access
$filename = 'example.txt';
$permissions = 0644; // For files
if (file_exists($filename)) {
    chmod($filename, $permissions);
    echo 'File permissions updated successfully.';
} else {
    echo 'File does not exist.';
}