0

I have a word document which I want the users to download from a link on site .

The code works fine on my localhost . However , when I upload the website on the server , instead of giving the option to save on the local machine , it opens up in the browser itself . Below is my code :

// Define the path to file

    $file = <filepath>;
    if(!file_exists($file))
    {
        die('Hard Copy does not exist on Server at the moment . Sorry for the inconvinience');
    }
    else
    {

    // required for IE, otherwise Content-Disposition may be ignored
        if(ini_get('zlib.output_compression'))
        ini_set('zlib.output_compression', 'Off');
        ob_clean();
        // Set headers
        header("Cache-Control: private");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: application/msword");
        header("Content-Transfer-Encoding: binary");

        readfile($file);
    }

Now , as I understand that , if I give Content-Disposition as attachment , it should give a prompt to save the file .

Also , I have added " AddType msword .doc " in my .htaccess file . Still it gives the same result .

Is there any additional setting which needs to be done on the server ?

saurabh
  • 1,730
  • 2
  • 15
  • 13

2 Answers2

1

Base On this : How to prevent caching in Internet Explorer i would advice you to add the following

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT\n");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header(sprintf("Content-Length: %d;\n"),filesize($file));

You can also add

header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate"); 

Apart from that .. your code works fine from here :

Baba
  • 94,024
  • 28
  • 166
  • 217
0

Try to add:

header('Expires: 0');
header('Content-Length: ' . filesize($file));
header('Content-Type: application/octet-stream');

EDIT

However, it works on my two different testing servers as expected with your code, so I think this is a server configuration issue rather than PHP code-level thing.

BudwiseЯ
  • 1,846
  • 2
  • 16
  • 28