-1

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.

  1. firefox => works perfect
  2. Opera => Downloads file as a HTM file
  3. Safari => 0kb File
  4. 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); 
www.amitpatil.me
  • 3,001
  • 5
  • 43
  • 61

3 Answers3

1

in my test of your code work perfectly if use local file instead url with url give me 0kb too

i test in firefox,IE,Chrome all result is same

  // content type for pptx file 
    $ctype = "application/vnd.openxmlformats-officedocument.presentationml.presentation";

    $file = "presentation.pptx";//attention  

    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); 
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
1

If file is in your server, don't use URL. If it is an external file, I would write it first to your server, and then print it to user.

I would also delete the header("Cache-Control: private",false); line; and use header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); instead of header("Expires: 0");. It always worked for me this way :)

Also try deleting ob_clean(); flush(); (as there is nothing to flush or clean before sending the file).

Marcelo Pascual
  • 810
  • 8
  • 20
1

Please note that there are some issue in IOS browsers for download.

Biswajit Maji
  • 869
  • 13
  • 23