2012-05-25 22 views
0

我決定嘗試使用mod_rewrite來隱藏用戶可以下載的文件的位置。mod_rewrite下載 - 使可疑的文件

所以他們點擊執導的一個鏈接「/下載/ SOME_FILE /」,他們反而得到「/downloads/some_file.zip」

實現這樣的:

RewriteRule ^download/([^/\.]+)/?$ downloads/$1.zip [L] 

這個工程除了他們當下載進度出現時,我得到一個文件「下載」,沒有看起來可疑的擴展名,用戶可能不知道他們應該解壓縮它。有沒有辦法做到這一點,所以它看起來像一個實際的文件?還是有更好的辦法,我應該這樣做?

提供一些隱藏文件位置的上下文/原因。這是一個樂隊,可以免費下載音樂,只要用戶註冊郵件列表即可。

也不是我需要的.htaccess

回答

1

內做到這一點,您可以通過發送Content-disposition頭設置文件名:

https://serverfault.com/questions/101948/how-to-send-content-disposition-headers-in-apache-for-files

+0

基於t他和一點谷歌搜索我試過了: RewriteRule^download /([^/\.]+)/?$ downloads/$ 1.zip [L,T = application/zip] 但它仍然沒有'工作 –

+0

更多信息。我從.htaccess 這樣做,所以這個使用LOCATION的例子不起作用 –

+0

設置'filename'是重要的部分。否則,瀏覽器將根據URL確定文件名。另一種方法是使用看起來像文件名的URL,例如'download/some-secret-value/filename.zip'並將其重定向到實際文件。 – Stefan

0

好了,所以我相信我的限制,以什麼頭文件我可以使用.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); 
?>