Hi,
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.
Code used
ob_start();
$file = "upload/".$filecode;
$actualfilename = $filecode;
if (file_exists($file))
{
$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-Type: application/x-msdownload');
//header("Content-Type: application/force-download");
//header("Content-Type: application/zip");
header('Content-Disposition: attachment; filename='.$actualfilename);
//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);
//$handle=fopen($file,"r") or exit("Unable to open file!"); //This code is not tested
exit;
}
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.
Please help!