好的,所以我相信我可以使用 .htaccess 设置哪些标头
所以我改用php解决了这个问题。
我最初复制了一个在这里找到的下载 php 脚本:
How to rewrite and set headers at the same time in Apache
但是我的文件太大了,所以这不能正常工作。
经过一番谷歌搜索后,我发现了这个:http ://teddy.fr/blog/how-serve-big-files-through-php
所以我的完整解决方案如下......
首先发送请求下载脚本:
RewriteRule ^download/([^/\.]+)/?$ downloads/download.php?download=$1 [L]
然后获取完整的文件名,设置标题,并逐块提供:
<?php
if ($_GET['download']){
$file = $_SERVER['DOCUMENT_ROOT'].'media/downloads/' . $_GET['download'] . '.zip';
}
define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk
// Read a file and display its content chunk by chunk
function readfile_chunked($filename, $retbytes = TRUE) {
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, CHUNK_SIZE);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
$save_as_name = basename($file);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header("Content-Type: application/zip");
header("Content-Disposition: disposition-type=attachment; filename=\"$save_as_name\"");
readfile_chunked($file);
?>