What are the best practices for managing file permissions in PHP for a download manager?

When managing file permissions in PHP for a download manager, it is important to ensure that only authorized users can access and download files. This can be achieved by setting appropriate file permissions and implementing access control mechanisms within the PHP code.

// Example code snippet for managing file permissions in PHP for a download manager

// Check if the user is authorized to download the file
if ($user->isAuthorized()) {
    $file = '/path/to/downloaded/file.txt';
    
    // Set file permissions to allow read access only
    chmod($file, 0644);
    
    // Send the file for download
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-Length: ' . filesize($file));
    
    readfile($file);
} else {
    // Handle unauthorized access
    echo 'You are not authorized to download this file.';
}