What are some potential pitfalls of directly linking to a file for download in PHP?

One potential pitfall of directly linking to a file for download in PHP is exposing sensitive information or files to unauthorized users. To prevent this, you should validate the user's access rights before allowing the download to proceed. This can be done by checking the user's authentication status and permissions before serving the file.

<?php
// Check user authentication and permissions before allowing download
if($user_authenticated && $user_has_permissions) {
    $file_path = '/path/to/file.pdf';
    
    if(file_exists($file_path)) {
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename="file.pdf"');
        readfile($file_path);
        exit;
    } else {
        echo 'File not found.';
    }
} else {
    echo 'You are not authorized to download this file.';
}
?>