What potential issues can arise when using Readfile() to download a file in PHP, and how can they be resolved?

Potential issues that can arise when using Readfile() to download a file in PHP include memory exhaustion when trying to read large files, lack of error handling for failed downloads, and potential security risks if the file path is not properly sanitized. To resolve these issues, it is recommended to use appropriate headers to handle large file downloads, implement error handling to deal with download failures, and sanitize the file path to prevent directory traversal attacks.

$file = 'path/to/file.pdf';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-Length: ' . filesize($file));
    
    readfile($file);
    exit;
} else {
    echo 'File not found.';
}