通过CloudFlare下载gzip压缩文件

时间:2013-03-21 11:55:53

标签: php http-headers gzip cloudflare

我有一个PHP文件保护程序脚本:

的.htaccess

RewriteEngine on
RewriteRule ^(.*).(zip|tgz)$ ../wp-file-protector.php?file=$1.$2 [QSA]

WP-文件protector.php

<?php

global $wpdb;
/** Make sure that the WordPress bootstrap has run before continuing. */
require( dirname(__FILE__) . '/wp-load.php' );


$current_user = wp_get_current_user();
if ($current_user->ID > 0){

  $order = $_GET['order'];
  $items = new_get_order_by_id_and_user($order, $current_user->ID);
  if($items != false){
    $file_enabled = true;
//Some authentication
    $filepath = dirname(__FILE__).'/../downloads/'.$_GET['file'];
    if($file_enabled && file_exists($filepath)){
      $filename = basename($filepath);
      $extension = end(explode('.', $filename));
      ob_clean();
      // http headers for zip downloads
      header("Pragma: public");
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: public");
      header("Content-Description: File Transfer");
      if($extension == 'tgz'){
        header("Content-Type: application/gzip");
      }else{
        header("Content-type: application/octet-stream");
      }
      header("Content-Disposition: attachment; filename=\"".$filename."\"");
      header("Content-Transfer-Encoding: binary");
      header("Content-Length: ".filesize($filepath));
      ob_end_flush();
      @readfile($filepath);
    }
  }
}

我使用zipped和其他文件工作正常。 gzip压缩文件出现问题。我得到了意想不到的结果(Firefox下载和打开操作也有不同的结果)。

我认为我需要一些用于gzip压缩文件的好标头。我该怎么用?

也很高兴知道请求正在通过Cloudflare服务运行,所以也许我应该发送一些标题,告诉cloudflare服务器不要gzip它。

我被困住了:(

更新#1

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

这也行不通。当我在firefox中选择save时,会告诉我什么时候打开它会损坏。当我选择打开时,我得到了一个没有扩展名的文件,但它似乎是焦油,因为我可以深入到目录。

0 个答案:

没有答案