0

サイトのリンクからユーザーにダウンロードしてもらいたいワード文書があります。

コードは私の localhost で正常に動作します。しかし、Web サイトをサーバーにアップロードすると、ローカル マシンに保存するオプションが表示されず、ブラウザ自体で開きます。以下は私のコードです:

// 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);
    }

今、私が理解しているように、Content-Disposition を添付ファイルとして指定すると、ファイルを保存するプロンプトが表示されるはずです。

また、.htaccess ファイルに「AddType msword .doc」を追加しました。それでも同じ結果が得られます。

サーバーで行う必要がある追加の設定はありますか?

4

2 に答える 2

1

これに基づいて:Internet Explorerでキャッシュを防止する方法次を追加することをお勧めします

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));

追加することもできます

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

それとは別に..あなたのコードはここからうまく動作します:

于 2012-10-14T23:42:58.223 に答える
0

追加してみてください:

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

編集

ただし、コードで期待どおりに 2 つの異なるテスト サーバーで動作するため、これは PHP コード レベルの問題ではなく、サーバー構成の問題だと思います。

于 2012-10-14T23:46:56.327 に答える