如何加密文件下载路径

时间:2011-06-16 08:48:37

标签: php javascript

我想允许用户从我的某个文件夹下载pdf文件。当用户“鼠标悬停”到文件下载图标时,有什么方法可以隐藏用户的下载路径吗?

使用PHP或javascript建议任何方法。

6 个答案:

答案 0 :(得分:4)

将浏览器指向PHP脚本,传递代表要下载的文件名的密钥。解码密钥,send the file via the PHP script

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

  

在某些时候,您必须为浏览器提供真正的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;

您可以使用?filename = test.foo调用download.php,然后下载/the/path/to/your/files/test.foo,这是不可公开访问的。

答案 3 :(得分:0)

是的,当用户点击导致“#”的链接时,您可以使用javascript重定向用户,例如

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

但是,这是毫无意义的,因为始终可以跟踪正在下载的文件(例如,使用HTTP嗅探器或其他工具)。从本质上讲,你所要求的是不可能和不合理的。

如果您需要确保只有某些人访问该文件,请让他们登录并检查凭据,然后再向他们提供数据。隐藏这条路是不可能的。

答案 4 :(得分: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');

}

答案 5 :(得分:-1)

正如其他人所指出的那样,在某些时候,你必须揭示网址,但如果要做的就是将其隐藏在状态栏中,你可以这样做:

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