0

以下のダウンロードスクリプトをテストしていました。ダウンロードは正常に機能しますが、ダウンロードした zip または rar アーカイブは常に破損しており、開くことができません。ローカルの開発サーバーとホスティング アカウントでテストしました。

私はこれがどのように機能するかを学ぼうとしていますが、本当に理解していません。

すべての助けに感謝します!

テストコード:

<?php
$is_logged_in = 1;
$path_to_file = 'downloads/tes.zip';
$file_name = 'test.zip';

if ($is_logged_in == 1)
{
    header("X-Sendfile: $path_to_file");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$file_name\"");
    exit;
}
?>

<h1>Permission denied</h1>
<p>Please Login first!</p>
4

2 に答える 2

1

ほとんどの場合、ファイルに何かが追加/追加されている可能性があります。バッファリングとクリーニングを使用してみてください。

<?php
ob_start();
$is_logged_in = 1;
$path_to_file = 'downloads/tes.zip';
$file_name = 'test.zip';

if ($is_logged_in == 1)
{
    $fp = fopen($path_to_file, 'rb');

    if(is_resource($fp))
    {
            ob_clean();
            header("Content-Type: application/force-download");
            header("Content-Length: " . filesize($path_to_file));
            header("Cache-Control: max_age=0");
            header("Content-Disposition: attachment; filename=\"$file_name\"");
            header("Pragma: public");
            fpassthru($fp);
            die;
    }
} else {
    echo "<h1>Permission denied</h1>";
    echo "<p>Please Login first!</p>";
}
于 2011-03-24T00:42:34.517 に答える
0

mod_xsendfileをロードするように Web サーバー (特に Apache) を構成しましたか? そのモジュールがインストールされていないと、スクリプトは基本的に何もしません。

于 2011-03-24T00:50:49.053 に答える