PHP:只允许文件下载到授权会员


PHP: Allow file download to a authorized members only

我有一个网站,我希望只有注册成员才能下载特定的文件。为此,我有以下结构:

在我拥有原始安装文件的目录中,我创建了一个.htaccess文件,其中包含:

Order Deny,Allow
Deny from all

并且,从一个名为download.php的文件中,我使用调用该文件

<?php
$filename = "127.0.0.1/eye/setup/setup.msi";
if(ini_get('zlib.output_compression'))ini_set('zli b.output_compression', 'Off');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename='"".basename($filename)."'";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

但是,问题是:如果有人知道我的文件的确切位置,他/她可以很容易地在他的/她的服务器上设置一个类似的PHP文件,并将$filename作为原始位置。那么,我怎样才能依靠这个脚本来实现安全性呢?

还有其他策略吗?

一种解决方案是将下载文件夹保留在DocumentRoot之外。然后download.php将使用本地路径读取它

$filename = "/path/to/eye/setup/setup.msi"; // or build filename using $_GET

然后,这个php代码将从本地路径读取所需文件的内容,并设置适当的内容类型。

您可以借助htpass文件中的htpass保护您的目录密码。在htaccess 中的这个htpass的帮助下