2011-06-16 197 views
2

我想让用户从我的文件夹下载pdf文件。当用户“mouseover”到文件下载图标时,有什么办法可以隐藏用户的下载路径吗?如何加密文件下载路径

建议使用PHP或JavaScript的任何方法。

+3

不。总有一种方法可以找出你从哪里下载文件(例如检查HTTP头文件)。你为什么想这样做? – 2011-06-16 08:50:33

+0

我不希望用户直接访问文件,如果下载链接是文件名MP(1).pdf,那么它表示该文件夹将有MP(2).pdf,MP(3).pdf,我不想要我的客户访问其他文件。 – fawad 2011-06-16 08:57:56

+1

然后通过PHP脚本(如@Ignacio建议)提供文件并将文件存储在Web根目录之外。 – 2011-06-16 08:59:54

回答

2

在有一点你必须给浏览器一个真正的URI,以便它能够获取文件。 试图隐瞒它是毫无意义的。

我不相信这是真的。有一种简单的方法可以使用PHP的fpassthru来保护服务器上的文件免受无特权的下载。如果你有一个叫做的download.php文件,其内容如下:?

<?php 

/** 
* Make sure the downloads are *not* in a publically accessible path, otherwise, people 
* are still able to download the files directly. 
*/ 
$filename = '/the/path/to/your/files/' . basename($_GET['filename']); 

/** 
* You can do a check here, to see if the user is logged in, for example, or if 
* the current IP address has already downloaded it, the possibilities are endless. 
*/ 


if(file_exists($filename)) { 
    /** 
    * Send some headers indicating the filetype, and it's size. This works for PHP >= 5.3. 
    * If you're using PHP < 5.3, you might want to consider installing the Fileinfo PECL 
    * extension. 
    */ 
    $finfo = finfo_open(FILEINFO_MIME); 
    header('Content-Disposition: attachment; filename= ' . basename($filename)); 
    header('Content-Type: ' . finfo_file($finfo, $filename); 
    header('Content-Length: ' . filesize($filename)); 
    header('Expires: 0'); 
    finfo_close($finfo); 

    /** 
    * Now clear the buffer, read the file and output it to the browser. 
    */ 
    ob_clean(); 
    flush(); 
    readfile($filename); 
    exit; 
} 

header('HTTP/1.1 404 Not Found'); 

echo "<h1>File not found</h1>"; 
exit; 

你可以调用的download.php与文件名= test.foo,它会下载/的/路径/要/你/files/test.foo,这是不公开的。

+3

直到它们传递'?filename = ../../../../../../etc/passwd' ... – 2011-06-16 09:06:40

+0

为这段代码添加一些理智检查。否则,一个好的答案。 – 2011-06-16 09:10:48

+0

@Ignacio:刚刚注意到:'basename(“../../../../../../../ etc/passwd”)'返回'passwd',而不是'/ etc/passwd' 。 – 2011-06-16 09:14:21

2

在某些时候,您将不得不为浏览器提供真正的URI,以便获取文件。试图隐瞒它毫无意义。

如果您想限制谁可以访问它,请在允许访问下载之前设置某种时间有限的凭据并进行身份验证。

+0

如果我必须提供真实的URI,是否可以使用.htaccesss文件阻止对文件夹的直接访问? – fawad 2011-06-16 08:51:52

+0

如果您的意思是“是否可以停止使用.htaccess文件提供目录列表文档的服务器”,那么是的。 '选项-Indexes' IIRC。 – Quentin 2011-06-16 08:53:02

+0

但是您确定客户端可以通过链接访问文件,但不能通过HTTP请求(直接路径)访问文件。 – fawad 2011-06-16 09:00:10

0

是的,您可以使用JavaScript重定向用户,当他们点击导致'#'的链接时,例如

<a href="#">Secret File</a> 

然而,这是没有意义的,因为它永远是可以跟踪正在下载什么文件(通过使用HTTP嗅探器,例如,或其他工具)。从本质上讲,你要求的是不可能和不合理的。

如果您需要确保文件仅由某些人访问,请让他们登录并在向他们提供数据前检查凭据。隐藏这条道路并不是要走的路。

+0

是的,你是对的。 – fawad 2011-06-16 09:00:51

-1

像其他人士指出,在某些时候,你必须显示的URL,但是如果你想要做的是从状态栏隐藏它,你可以这样做:

<a href="http://example.org" onmouseover="window.status='Add something here.'; return true;" onmouseout="window.status=''; return true;">Description</a> 
+0

谢谢,这也是一个非常好的方法。 – fawad 2011-06-16 09:00:37

+0

在FireFox中按CTRL + U。 – 2011-06-16 09:12:10

0
Before Passing FilePath it to download_file() fucntion. Append the path to file id. Like Below. 


$FilePaths='../Uploaded Files/'.$FilePath; 

download_file($FilePaths); 

function download_file($fullPath) 
{ 

    // Must be fresh start 
    if(headers_sent()) 
    die('Headers Sent'); 

    // Required for some browsers 
    if(ini_get('zlib.output_compression')) 
    ini_set('zlib.output_compression', 'Off'); 

    // File Exists? 
    if(file_exists($fullPath)){ 

    // Parse Info/Get Extension 
    $fsize = filesize($fullPath); 
    $path_parts = pathinfo($fullPath); 
    $ext = strtolower($path_parts["extension"]); 

    // Determine Content Type 
    switch ($ext) { 
     case "pdf": $ctype="application/pdf"; break; 
     case "exe": $ctype="application/octet-stream"; break; 
     case "zip": $ctype="application/zip"; break; 
     case "doc": $ctype="application/msword"; break; 
     case "xls": $ctype="application/vnd.ms-excel"; break; 
     case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
     case "gif": $ctype="image/gif"; break; 
     case "png": $ctype="image/png"; break; 
     case "jpeg": 
     case "jpg": $ctype="image/jpg"; break; 
     default: $ctype="application/force-download"; 
    } 

    header("Pragma: public"); // required 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: $ctype"); 
    header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";"); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".$fsize); 
    ob_clean(); 
    flush(); 
    readfile($fullPath); 

    } 
    else 
    die('File Not Found'); 

} 
相关问题