I found this code that allows me to opens a window and allows the user to download a file (download.php)
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
It works well to allow the user to download a file from the server via a pop up window, but I have two issues:
Before executing this block of code, I have some text printing on the screen (browser). However, when the .php file is executed with this block at the very end, nothing prints on the screen, and it goes directly to the download. How do I ensure that things are printed on the browser and then allow the file to be downloaded?
Then, I thought maybe I'll instead insert a link after things are printed, but I'm having trouble getting this to work. When I click on the link to download (href='download.php'), a blank screen is returned and nothing else.
Could someone chime in and help? TIA!