I am working on script which allows user to download pptx and zip files using php, but this script behaves different on different browsers. I tried many scripts available on the internet, but nothing worked properly so I made this one collecting chunks from different scripts.
- firefox => works perfect
- Opera => Downloads file as a HTM file
- Safari => 0kb File
- IE => Catching old file
My Code:-
// content type for pptx file
$ctype = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
$file = "http://www.abc.com/presentation.pptx";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: ".$ctype);
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
ob_clean();
flush();
readfile( $file);
How can I get all browsers to reliably download the file instead of displaying the seemingly random behaviour above?
Edit: Below is the code that worked for me, I am not sure what was the problem, unwanted headers or file path ? I made both the changes and it worked.
$ctype = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
$file = "Presentation3.pptx";
header("Pragma: public");
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: ".$ctype);
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile( $file);