如何加密文件下载路径

how to encrypt file download path

本文关键字:文件下载 路径 加密 何加密      更新时间:2023-09-26

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

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

将浏览器指向PHP脚本,传递一个表示要下载的文件名的键。解码密钥,并通过PHP脚本发送文件。

在某个时刻,您必须向浏览器提供真实的URI,以便它能够获取文件。试图隐瞒是没有意义的。

我不相信这是真的。有一种简单的方法可以保护服务器上的文件不受使用PHP的fpassthrough的非特权下载的影响。如果您有一个名为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?filename=test.foo,它将下载/path/to/your/files/test.foo,这是不可公开访问的。

在某个时刻,您必须向浏览器提供真实的URI,以便它可以获取文件。试图隐瞒是没有意义的。

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

是的,当用户单击指向"#"的链接时,您可以使用javascript重定向用户,例如

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

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

如果您需要确保文件仅由某些人访问,请让他们登录并检查凭据,然后再向他们提供数据。隐藏路径不是一条路。

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');
}

正如其他人所指出的,在某些时候,你必须显示URL,但如果你只想在状态栏中隐藏它,你可以这样做:

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