PHP ファイル保護スクリプトがあります。
.htaccess
RewriteEngine on
RewriteRule ^(.*).(zip|tgz)$ ../wp-file-protector.php?file=$1.$2 [QSA]
wp-file-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);
}
}
}
圧縮されたファイルやその他のファイルで問題なく動作します。この問題は、gzip ファイルで発生します。予期しない結果が得られました (Firefox のダウンロードと開くアクションでも結果が異なります)。
gzip されたファイルには適切なヘッダーが必要だと思います。何を使えばいいですか?
また、リクエストがCloudflareサービスを介して実行されていることを知っておくとよいので、gzipしないようにcloudflareサーバーに指示するヘッダーを送信する必要があるかもしれません.
私は立ち往生しています:(
更新 #1
header("Content-type: application/x-gzip");
これも機能しません。Firefox に保存を選択すると、開いたときに破損していることがわかります。開くを選択すると、拡張子のないファイルができましたが、ディレクトリをさらに深くできるため、tarのようです。