What potential issue arises from using "exit();" in the download process?

Using "exit();" in the download process can prematurely terminate the script, preventing any further code execution. This can lead to incomplete downloads or missing cleanup tasks. To solve this issue, you can use "readfile()" to send the file to the user and then use "unlink()" to delete the file after it has been successfully downloaded.

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

if (file_exists($file)) {
    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);
    
    unlink($file); // Delete the file after successful download
}