I have used below file download php script which works fine on my PC. But when I published this code to server, it throws two errors
- Warning: Cannot modify header information - headers already sent by... To avoid this warning I have used ob_start();... which helped to remove these warnings but major problem is
- when clicked download button, it shows text which is not in readable format.
$file = "upload/".$filecode;
$actualfilename = $filecode;
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/inc/";
$con = $db->fnInitConn();
header('Content-Description: File Transfer');
//Below line is replace by force-download because this was not working when downloaded from web server
header('Content-Disposition: attachment; filename='.$actualfilename);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Length: ' . filesize($file));
//$handle=fopen($file,"r") or exit("Unable to open file!"); //This code is not tested
I have kept some commented code just to show that I have tried multiple options. It's not just that an executable or zip file not able to download but other files like TXT or EXCEL are showing same non readable text.