PHP如何使用下载按钮下载tar.gz文件


PHP How to download file tar.gz using download button?

我有一个这样的代码:

        header("Pragma: public", true);
        header("Expires: 0"); // set expiration time
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header('Content-type: application/rar');
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header('Content-Type: application/download');
        header('Content-Disposition: attachment; filename='.$fullname);
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . filesize($fullname));
        $fp = fopen($fullname, "r");
        fpassthru($fp);
        fclose($fp);
根据上面的代码,下载正在运行,但是无法打开tar.gz文件。如何解决?可以下载tar.gz吗?

GNU压缩档案的内容类型为application/x-gzip。您应该使用下面的代码来设置内容类型

header('Content-type: application/x-gzip');